The most thorough way to catch Aires is by using the match expression we learned about earlier on in
this course.
So let's create one so we can say let file equals match file.
And then if an OC is returned with a file, then just continue on with the file.
But if an error is returned, then let's panic air and then let's also pass an error message that it
gives us
so we know that we don't have an error text file to open.
So we know that this is going to air out.
So if we run this.
We see that it panicked at with our air message air and then we see OS code four operating system code
two kind not found and in the message the system cannot find the file specified.
So we see that the error printed out an error kind of not found, which is information we can do something
with.
Air kind is provided by the standard library.
So let's try to match on air kind.
So what we can do is we can come up here and we can say use standard IO and then air kind, and now
we can match on air kind so we can delete this and say Match air dot kind.
And now we have another match statement in here so we can say air kind and then not found and then match
on this and say, because we want to create a new file called Air Text, so we want to create air dot
text.
And then because this can also fail, we want to say, okay, file
created and then just say file created.
Otherwise we got an air here as well and we want to panic and it says cannot create the file.
And now we have to have an exhaustive match.
So down here we'll just do an underscore and say panic.
It was some other air kind, which we know in this case it won't happen because we already know the
air that we're going to get.
So now if we run this.
We then air out and we see that air act was created for us.
So great.
So that's how we can use match statements and we can even nest them to be able to handle logic for us.
So.
Another tool we have at our disposal is called Unwrap.
So I'm going to.
Comment.
All of this out.
And I'm actually going to just quickly rename this and just call it Air two.
That way we can keep generating airs and that we can come up here and we can say unwrap and.
What's going to happen in UNWRAP is if OC is returned from the method, then unwrap will return the
value inside of oak forests.
But if the result is an error, unwrap will call panic for us.
So since we no longer have air, I expect this to panic force, which it does.
And it said the thread main panicked at our unwrap unwrap method on an air value and then our normal
air code.
And then if we change this back to be a file that will be found, we no longer have an air.
So if we want to customise our air, then we can use something called the expect.
So instead of unwrap, we say expect and then we pass in a string air opening the file and then we get
rid of these extra parentheses.
So again, we know that this is going to air out because we changed our file name again.
So now if we run this, we see that it says air opening the file and also gave us all the extra information
regarding this.
So expect is is beneficial because it's going to allow us to much more quickly track down the source
of the panic call in any of our logs.
So in the next lecture, we're going to look at how we can propagate air's.