我正在panic::catch_unwind惊慌失措:
use std::panic;
fn main() {
let result = panic::catch_unwind(|| {
panic!("test panic");
});
match result {
Ok(res) => res,
Err(_) => println!("caught panic!"),
}
}
Run Code Online (Sandbox Code Playgroud)
(游乐场)
这似乎工作得很好,但我仍然得到了恐慌的输出到stdout.我想这只打印出来:
caught panic!
Run Code Online (Sandbox Code Playgroud)
代替
thread '<main>' panicked at 'test panic', <anon>:6
note: Run with `RUST_BACKTRACE=1` for a backtrace.
caught panic!
Run Code Online (Sandbox Code Playgroud)
She*_*ter 15
您需要注册一个恐慌钩与std::panic::set_hook不做任何事.然后你可以抓住它std::panic::catch_unwind:
use std::panic;
fn main() {
panic::set_hook(Box::new(|_info| {
// do nothing
}));
let result = panic::catch_unwind(|| {
panic!("test panic");
});
match result {
Ok(res) => res,
Err(_) => println!("caught panic!"),
}
}
Run Code Online (Sandbox Code Playgroud)
正如Matthieu M.所说std::panic::take_hook,如果你需要,你可以获得当前的钩子,以便之后恢复它.
也可以看看:
使用以下命令catch_unwind_silent而不是常规命令catch_unwind来实现预期异常的静默:
use std::panic;
fn catch_unwind_silent<F: FnOnce() -> R + panic::UnwindSafe, R>(f: F) -> std::thread::Result<R> {
let prev_hook = panic::take_hook();
panic::set_hook(Box::new(|_| {}));
let result = panic::catch_unwind(f);
panic::set_hook(prev_hook);
result
}
Run Code Online (Sandbox Code Playgroud)