mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:14:32 -06:00
Wording tweaks
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
# Error Handling
|
||||
|
||||
Rust's laser-focus on safety spills over into a related area: error handling.
|
||||
Errors are a fact of life in software. Rust has a number of tools that you can
|
||||
use to handle things when something bad happens.
|
||||
Errors are a fact of life in software, so Rust has a number of tools 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 safer and more robust by
|
||||
eliminating the possibility of unexpected errors being discovered late in the
|
||||
development process.
|
||||
|
||||
Rust splits errors into two major kinds: errors that are recoverable, and
|
||||
errors that are not recoverable. What does it mean to "recover" from an
|
||||
@@ -14,4 +18,4 @@ error? In the simplest sense, it relates to the answer of this question:
|
||||
The technique that you use depends on the answer to this question. First,
|
||||
we'll talk about `panic!`, Rust's way of signaling an unrecoverable error.
|
||||
Then, we'll talk about `Result<T, E>`, the return type for functions that
|
||||
may return an error, but one you can recover from.
|
||||
may return an error you can recover from.
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
Sometimes, bad things happen, and there's nothing that you can do about it. For
|
||||
these cases, Rust has a macro, `panic!`. When this macro executes, your program
|
||||
will terminate execution, printing a failure message and then quitting. The most
|
||||
common reason for this is when a bug of some kind has been detected, and it's not
|
||||
clear how to handle the error.
|
||||
will print a failure message and then quit. The most common reason for this is
|
||||
when a bug of some kind has been detected, and it's not clear how to handle the
|
||||
error.
|
||||
|
||||
Let's try it out with a simple program:
|
||||
Let's try it out with a simple program:
|
||||
|
||||
```rust,should_panic
|
||||
fn main() {
|
||||
@@ -43,7 +43,7 @@ fn main() {
|
||||
|
||||
We attempt to access the hundredth element of our vector, but it only has three
|
||||
elements. In this situation, Rust will panic. Using `[]` is supposed to return
|
||||
a number. But if you pass it an invalid index, there's no number Rust could
|
||||
an element. But if you pass it an invalid index, there's no number Rust could
|
||||
return here, it would be wrong. In this case, we've typed in a literal, so in
|
||||
theory, Rust could figure it out. But if our program was different, say, maybe
|
||||
reading the index from user input, it would not be possible to determine at
|
||||
|
||||
Reference in New Issue
Block a user