Catch panic message

Hi,

I found how to catch a potential panic when launching some function with :

 let potential_panics_return = tokio::spawn(async move { my_fn().await });

So I can continue my program and it is great.

But same if I catch the panic, a print of the panic appears in my console... And I would like to catch the message printed to in order to not print it now.

Do you know if this action is possible and how ?

rust panic can have arbitrary (well, not really arbitrary, see panic_any() for details) payload data, but String or &'static str are most common, which are the payload types when you use the panic!() macro. see documentation of panic!() for details.

to catch the panic, use catch_unwind().

note, the default panic hook will print a line to the console before the panic was caught. if you want to suppress this message, you can install a custom panic hook. see set_hook(). note: you can re-install the default hook with take_hook().

example:

2 Likes