forked from rust-lang/book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edits to chapter 9 as a result of feedback from nostarch
- Loading branch information
1 parent
7d2a7b4
commit 4610e53
Showing
3 changed files
with
237 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
# Error Handling | ||
|
||
Rust's focus on reliability extends to the area of error handling. Errors are a | ||
fact of life in software, so Rust has a number of features that you can use to | ||
handle situations in which something bad happens. In many cases, Rust requires | ||
you to acknowledge the possibility of an error occurring and take some action | ||
in that situation. This makes your program more robust by eliminating the | ||
possibility of unexpected errors only being discovered after you've deployed | ||
your code to production. | ||
Rust's commitment to reliability extends to error handling. Errors are a fact | ||
of life in software, so Rust has a number of features for handling situations | ||
in which something goes wrong. In many cases, Rust will require you to | ||
acknowledge the possibility of an error occurring and take some action before | ||
your code will compile. This makes your program more robust by ensuring that you | ||
won't only discover errors after you've deployed your code to production. | ||
|
||
Rust groups errors into two major kinds: errors that are *recoverable*, and | ||
errors that are *unrecoverable*. Recoverable errors are problems like a file not | ||
being found, where it's usually reasonable to report that problem to the user | ||
and retry the operation. Unrecoverable errors are problems like trying to | ||
access a location beyond the end of an array, and these are always symptoms of | ||
bugs. | ||
Rust groups errors into two major categories: *recoverable* and *unrecoverable* | ||
errors. Recoverable errors are situations when it's usually reasonable to | ||
report the problem to the user and retry the operation, like a file not being | ||
found. Unrecoverable errors are always symptoms of bugs, like trying to access | ||
a location beyond the end of an array. | ||
|
||
Most languages do not distinguish between the two kinds of errors, so they | ||
handle both kinds in the same way using mechanisms like exceptions. Rust | ||
doesn't have exceptions. Instead, it has the value `Result<T, E>` to return in | ||
the case of recoverable errors and the `panic!` macro that stops execution when | ||
it encounters unrecoverable errors. This chapter will cover the more | ||
straightforward case of calling `panic!` first. Then, we'll talk about | ||
returning `Result<T, E>` values and calling functions that return `Result<T, | ||
E>`. Finally, we'll discuss considerations to take into account when deciding | ||
whether to try to recover from an error or to stop execution. | ||
Most languages don't distinguish between the two kinds of errors, and handle | ||
both in the same way using mechanisms like exceptions. Rust doesn't have | ||
exceptions. Instead, it has the value `Result<T, E>` for recoverable errors and | ||
the `panic!` macro that stops execution when it encounters unrecoverable | ||
errors. This chapter will cover calling `panic!` first, then talk about | ||
returning `Result<T, E>` values. Finally, we'll discuss considerations to take | ||
into account when deciding whether to try to recover from an error or to stop | ||
execution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.