>> Error handling is the process of
anticipating and working with the possibility of failure.
Learning how to manage errors effectively Rust before they come
a problem will be extremely helpful at saving you time and sanity.
In this video, I'll describe
the multiple ways to handle errors and Rust.
Will focus on using panic to deal with unrecoverable errors,
option for possible non-existent values,
and Result for handling other things that might go wrong.
Panicking is the simplest way to handle errors and Rust.
You can use the panic macro to quit the program execution.
Using a panic macro would look like this.
Wherever we want inside of a mean function,
we can include the panic macro,
along with a message to print out
when the program stops executing.
What happens when the panic macro executes?
First, a failure message will be printed,
then the program unwinds and cleans
up the stack, and then it quits.
Generally, a panic macro should only be
used when a program comes to an unrecoverable state.
Rust can also emit a panic in some operations such as division
by zero or an attempt to access an index that isn't an array,
a vector, or a HashMap.
Take for example, this piece of code.
Here we have a vector with item 0, 1, 2 and 3,
and then we try to print out the value of v index six.
This will cause a panic because there is no v of index six,
the maximum index that we have in this vector is three.
Many errors aren't so bad that
the program needs to stop and panic.
Maybe you encounter a recoverable error or
you need to handle the absence of values.
The option T, as introduced in the enum video,
manages the possibility of a null or non-existent value.
The option enum is defined as the following.
We have an enum called option T with two variants; none and some.
The T part of option T states that
the type T is a generic and associated with the some variant,
none indicates that no element was found,
and some means that an element of type T was found.
If the value exists as a specified index, it's rapidly option,
some value where the value is
type T. If the index is out of bounds,
it will return an option none value and instead.
For other recoverable errors that are more common,
the Result type can be used.
Examples of recoverable errors might be when a function
fails or an operation that you expect to work doesn't.
Maybe your program tries to open a file that doesn't yet exist.
Luckily, Rust provides the Result enum
for returning and propagating errors.
The Results enum is defined as the following.
We have an enum name Result with type T
and E. These are both generic types and then variants that
the Result enum has are Ok and T.
The Ok T variant represents a success and contains a value.
The Err E variant represents an error.
The Result enum is principally
used for input and output operations,
such as parsing strings into other types,
file access and data validation.
Contrast to the option type,
which describes the possibility of the absence of a value,
the Result type is best suited whenever failures are expected.
The Result type also has the unwrap and expect helper methods.
If the Result value is the Ok variant,
the unwrap method will return the value inside Ok,
if the Result is the Err variant,
unwrap will call the panic macro for us.
The expect method is used in a similar way as unwrap,
or it either returns a value or calls the panic macro.
When it calls the panic macro,
it allows us to also write a detailed error message.
The last thing I want to call out is the question mark operator.
This operator works similar to how match statements work,
but eliminates the need for an entire statement,
and only a question mark is used.
The question mark operator applies to
a Result value and
unwraps the inner value if it was the Ok variant.
If the Result is the Err variant,
it returns from the point of the function you're currently in.
The question mark operator can also be used with the option enum,
where a value will be returned for Some variant of
T and none will be returned for the None variant.
Instead of needing a match statement,
only the question mark operator is needed.
This makes code the cleaner and still allows us to handle
errors nicely passing them up the call stack.
That is the overview of Rust error handling features.
The various options for error handling exist
to help you write more robust code.
Becoming familiar with when and how to use these error types can
make all the difference in ensuring
your code is reliable and safe.
In the next video,
I'll explore more code examples
using the various forms of error handling.
[MUSIC]