diff --git a/anti_patterns/catch_panic.md b/anti_patterns/catch_panic.md index b0ea2182..d03343d1 100644 --- a/anti_patterns/catch_panic.md +++ b/anti_patterns/catch_panic.md @@ -37,11 +37,35 @@ unexpected because they are bugs that should not happen. It would not make sense to handle an error for QuickSort returning an incorrectly unsorted array, as this should not happen. -There are scenarios where using panic::catch_unwind is the correct choice, eg, a +## Advantages + +There are scenarios where using `panic::catch_unwind` is the correct choice, e.g. a web server implementation wants to save an unwinding thread in order to send a valid response if the route for that request (as in: logic outside of the web server implementor's control) is producing a panic. +## Disadvantages +​ +`panic::catch_unwind` may not catch all panics in Rust. A panic in Rust is not always +implemented via unwinding, but can be implemented by aborting the process as well. +`panic::catch_unwind` only catches unwinding panics, not those that abort the process. + +Also note that unwinding into Rust code with a foreign exception +(e.g. a an exception thrown from C++ code) is undefined behavior. + +TODO: since Result::unwrap() converts the error to a string, it's harder to distinguish +between different kinds of errors than if we had matched the result directly. + +## Discussion + +TODO: +?-operator to propagate errors +explain why unwinding is bad +other disadvantages of panic::catch_unwind ++ "The example could be improved by adding a function and which panics and catching the panic +in the caller, then matching the Result. Describing the example you could show how by returning +a Result, the Result-ness of the function is described in the signature." + Expected errors should not result in stack unwinding. Instead, expected errors should be handled through the Result and Option types. [The Rust Book's chapter on Error Handling](https://doc.rust-lang.org/book/error-handling.html) elaborates further on this. @@ -49,6 +73,9 @@ on Error Handling](https://doc.rust-lang.org/book/error-handling.html) elaborate ## See also [The Rust Book: Error Handling](https://doc.rust-lang.org/book/error-handling.html) + [Rust 1.9 announcement, which contains a description of this antipattern](http://blog.rust-lang.org/2016/05/26/Rust-1.9.html) + [Result documentation](http://doc.rust-lang.org/std/result/enum.Result.html) + [panic::catch_unwind documentation](https://doc.rust-lang.org/std/panic/fn.catch_unwind.html)