Skip to content

Commit

Permalink
Added todos
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsan committed Jan 1, 2021
1 parent f53afc8 commit 9c738a6
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion anti_patterns/catch_panic.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,45 @@ 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.

## 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)

0 comments on commit 9c738a6

Please sign in to comment.