Welcome back.
So in this video we're going to look at the finally keyword.
And later on we're going to look at more exceptions right.
So just don't worry about it.
We just looked at this very simple exception keyword.
But now let's look at the finally keyword.
So the finally keyword allows you in the context of try and catch to execute code.
No matter if whatever you put into try worked or didn't work.
So if it worked, finally we'll be called.
If it didn't work, finally we'll be called.
So basically, it is perfect for a situation where let's say you want to close the download stream,
so you want to finalize some code, or you want to clean up some, some objects or so.
So for example, if you download something from the web, what you need to do is you need to open a
download stream and you download it and then you need to close that download stream or that data stream.
It's not the download stream, but a data stream.
You need to close that afterwards again.
Otherwise you can run into all kinds of errors along the way.
So the try block would, for example, try to open the stream and then would try to download something,
but then let's say something breaks along the way.
Then you don't want to have your data stream still be open all the time.
You want to make sure that it closes after you execute the that bit of code.
So that's where the finally block comes in.
And this is just one example obviously.
But let's just have a look at the finally keyword in our code.
So you can see it's in connection with this try catch.
Finally you could say it's like an else inside of the if else if and else.
So we have this finally keyword which will be called either way.
So it's not like in the elsewhere it's only called if everything else didn't happen.
So in this case the finally block will be called anyways no matter what.
So here let me just say something like this always executes.
Okay.
And where is this useful?
As I said, this is useful to clean up or finalize or to, uh, well end your connections.
So ideal for cleaning up resources like closing file streams or database connections.
So let's now run our code and see what's happening.
So we say please enter a number.
Let me enter a number.
And it says this always executes.
Okay let's run it again.
This time let's cause an error.
It says error attempted to divide by zero.
Then it says this always executes and result is zero.
Now let's cause another error.
Let's call this one by entering the wrong type.
So error.
The input string f was not in a correct format and it still says this always executes.
So you see that this finally block always executes no matter what.
And this is crucial for whenever you're working with databases or with streams or file streams, for
example, downloading data or even just getting something from the USB stick.
So loading something or even loading any data from your hard drive, even in that case something can
go wrong.
For example, your hard drive could break or, uh, the connection to your USB stick could break, or
the USB stick itself could break.
So in any of these cases, you don't want your application to crash.
So just imagine you're using your phone and you are storing or like taking a picture.
And while you're taking the picture, suddenly your PC or your phone entirely crashes.
Nothing works because for some reason it couldn't store the picture on your device.
So if that happens, you want it to have some fail safes.
And that's where our try catch and finally are super useful.
Okay, so now we've seen how that works.
Now let's look at throwing our own exceptions okay.
So see you in the next video.