How to catch all panics?

In python, normally, we can use below code to catch all errors, no matter how many level in the run(),
and we can easily get all erros under run() then review the code and improve it.

try:
    run()
except:
   print(traceback.formatexec)

how to have this style in rust ? I used below but maybe it cannot catch all errors, what's the best practise to do it ?

main(){
  let r:String = run();
  let info: String = match r {
         Ok(someresultstring) => println("{:?}", someresultstring),
         Err(errs) => println("{:?}", errs),
  }
}

fn run() -> Result<String, Errors>{
      dosomething.....

}

You can catch most panics using catch_unwind, but there are some rare situations where a panic will abort the program without recourse. In particular, any panic that fires while another is being handled is uncatchable, and there’s also a Cargo setting that makes all panics uncatchable.

5 Likes

Note that it's theoretically (and practically) possible to throw a value as panic payload which panics when dropped. Catching such a panic may cause a further panic once the payload is dropped. This can cause soundness issues if you really intend to catch every unwinding panic. See #86027.

1 Like

Note that panics in Rust (caught with catch_unwind if the panics are unwinding panics) are only meant for serious errors and/or bugs. Normal errors don't unwind the stack but just return from a fuction or method.

You can use return types like Result<T, Box<dyn std::error::Error>> or the anyhow crate to specify return types that contain arbitrary errors. The ? operator can escalate any error to the next function on the stack.

In that case you can handle errors in the outermost function simply by examining the Result that is returned by the function you called.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.