6

I am new to Rust and have come to understand Rust defaults to panics and not exceptions.

I have a rust project that depends on external libraries.

I have handled all unwraps and ?'s in my code using match statements, but I am not sure how to handle a panic by external libraries.

In other languages I would just catch an exception thrown by the library.

As Rust defaults to panics, the libraries don't return an exception but panic and thus abort execution of the thread.

I would ideally want to log and continue execution, not panic and abort.

I have tried the following:

  • catch_unwind, but this looks like something I can't use on an external library.
  • log-panics crate, which logs the panic using a panic hook. I am able to log the panic, but not prevent aborts.
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

1 Answers1

13

DON'T PANIC

I mean, that's the real solution: you must avoid panics, not try to recover from them when they happen.

Some languages casually use exceptions to deal with conditions preventing some operations, and manage them without crashing. In Rust, those unsupported conditions are managed with errors, not panics.

A panic in Rust is

  • most often a bug, usually temporary because you've put an unwrap in your first prototyping
  • or a very extraordinary condition

As the Book says:

When code panics, there’s no way to recover

The various utilities like catch_unwind are, at best, aiming at more gracefully quitting, they don't let your program run as if nobody happened.

When a crate you use panics, first check you're using the function as expected (and if you can't check that without panicking that's a bug in that lib), then either have it fixed or fix it yourself if you can.

There's no reasonable way to deal with a casual panic, apart from crashing as fast as possible. A panic isn't casual in the life of your program.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 17
    Sorry, I always wanted to write "don't panic" in large friendly letters. – Denys Séguret Apr 30 '21 at 08:57
  • 4
    May I suggest emphasizing that if a library provides no way to handle (or check for) a situation without panicking, that's a bug in the library's API design? – Kevin Reid Apr 30 '21 at 14:53
  • @KevinReid Trying to put that in the answer. – Denys Séguret Apr 30 '21 at 15:11
  • 2
    This is a non-answer. In my case something in web_sys or wasm_bindgen_futures is panicing, I can't rewrite these libraries and the panic is very hard to reproduce so it's also nearly impossible to produce a decent report for those teams. If I could simply catch the panic, I think it could just be ignored, instead of now have a poisoned mutex to deal with. – Clayton Rabenda Jun 02 '22 at 05:13
  • 1
    @ClaytonRabenda No, you shouldn't try to handle panics as what some other languages call exceptions. As the rust book [says](https://doc.rust-lang.org/book/ch09-03-to-panic-or-not-to-panic.html) "When code panics, there’s no way to recover". – Denys Séguret Jun 02 '22 at 09:08