mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 19:17:14 -06:00
fixed spelling
This commit is contained in:
@ -1,34 +1,34 @@
|
||||
# Error Handing
|
||||
|
||||
This is a factor of life in software,
|
||||
Rust has a number of features for handling errors. One feature is that Rust requires you to acknowledge the possibility of an error and take some action beofre our code will compile.
|
||||
Rust has a number of features for handling errors. One feature is that Rust requires you to acknowledge the possibility of an error and take some action before our code will compile.
|
||||
|
||||
This requirement ensures that errors are handled before the possiblity could arise
|
||||
This requirement ensures that errors are handled before the possibility could arise
|
||||
|
||||
This can be split into two major categories
|
||||
- [*Recoverable*](#recoverable-errors) - File a file not found, just need to report the problem to the user and retry the operation
|
||||
- [*Unrecoverable*](#unrecoverable-errors) - A symptom of bugs, like trying to access a location beyond the end of an array. Need to immediately stop the program
|
||||
|
||||
Many languages dont distinguish between the two kinds of errors and handle them the same way using mechanisms such as exceptions
|
||||
Many languages don't distinguish between the two kinds of errors and handle them the same way using mechanisms such as exceptions
|
||||
|
||||
Rust does not have exceptions
|
||||
|
||||
Instead it has the type `Result< T, E>` for recoverable errors
|
||||
|
||||
It has the `panc!` macro to stop eecution when an unrecoverable error occurs
|
||||
It has the `panc!` macro to stop execution when an unrecoverable error occurs
|
||||
|
||||
## Unrecoverable Errors
|
||||
Whne bad things happen in your code and nothing you can do nothing about it then Rust has the `panc!` macro
|
||||
When bad things happen in your code and nothing you can do nothing about it then Rust has the `panc!` macro
|
||||
|
||||
There are two ways to cause a panic:
|
||||
- by taking an action that causes the code to paic (like accessing an array past the end)
|
||||
- explicity calling `panic!` macro
|
||||
- by taking an action that causes the code to panic (like accessing an array past the end)
|
||||
- explicitly calling `panic!` macro
|
||||
|
||||
By default these print a failure message, unwind, clean up the stack and then quit.
|
||||
|
||||
Using an environment variable you can also have Rust display the call stack when a panic occurs. This can make it easier to track down the source of the panic
|
||||
|
||||
When a call to `panic!` occurs the error message will be contained in the last two lines. The first line will contain our message and the second is when te source of this panic occured
|
||||
When a call to `panic!` occurs the error message will be contained in the last two lines. The first line will contain our message and the second is when the source of this panic occurred
|
||||
|
||||
example
|
||||
```rust
|
||||
|
Reference in New Issue
Block a user