-
Notifications
You must be signed in to change notification settings - Fork 400
Adding catch_panic anti-pattern #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # panic::catch_unwind for exceptions | ||
|
|
||
| ## Description | ||
|
|
||
| This antipattern arises when the method for catching (`panic::catch_unwind`) an | ||
simonsan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| unexpected problem (implementation bug) is used to handle an expected problem, | ||
| such as a missing file. | ||
|
|
||
| ## Example | ||
|
|
||
| ```rust | ||
| use std::io::prelude::*; | ||
| use std::fs::File; | ||
| use std::panic; | ||
|
|
||
| fn main() { | ||
| // panic::catch_unwind catches any panic that occurs within the | ||
| // function passed to it | ||
| let result = panic::catch_unwind(|| { | ||
| let mut file_result = File::open("foo.txt"); | ||
| file_result.unwrap(); // causes a panic if the file is not found | ||
| }); | ||
|
|
||
| // the program continues running despite the panic | ||
| println!("potential error: {:?}", result); | ||
| assert!(result.is_err()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Motivation | ||
|
|
||
| In rust, there are two ways an operation can fail: An expected problem, like a | ||
| file not being found, or a HTTP request timing out. Or, an unexpected problem, | ||
| such as an index being out of bounds, or an assert!() failing. These are | ||
| 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 | ||
| 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. | ||
|
|
||
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add why stack unwinding is bad? And perhaps the other disadvantages of using catch_unwind? Also, where is it appropriate to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also: since |
||
|
|
||
| ## 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) | ||
simonsan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.