Hello, in this Java programming video, we are going to take a look at the finally block, which is attached to a try catch statement for do a simple toy patch, nothing that you haven't seen before, and I tend to have a zero, this will cause an error, we're doing division zero, you will catch it however, in a catch block, and it is a ception error, like so. And I'll just do a system dot article print out the error. I'm just gonna print out my own photo there and I'm very naughty. And if we run this day, this gets triggered and the application is still running. After this, we have the focus plugin, I will prove that to you by putting an end here, run it, and there we go. So that's pretty cool.
What we can have is something called a finally block that goes up the end. And this will be triggered irrespective of whether or not an exception is true. So for the system, dot out dot print ln. So coming here for run it, as you can see, it's being run. But if unless I do division by one, for example, which is acceptable, this doesn't get triggered. This exception doesn't therefore this code is not printed out, and you can see here, but the finally block still prints out or you know, execute its code.
So you're thinking what is the purpose of this? So for example, it's used For cleanup, so if you need to do some sort of satellite management, just cleanups, some variables or anything like that, that you will generally want to do at the end of the try block, but you have been unable to because a exception was caught. If you put it in the Finally, you will always get called. So if there's a bit of code that you absolutely need to make sure gets called, regardless of whether or not what's in the Troy block is successful, then you put it in the finally and that's it. If you want if you want to multi catch block as well, you can do that just put the catches right here as an extra task or recommend combining this code with the code from the multi catcher tutorial. If you have any questions, feel free to reach out and I look forward to seeing you in the next awesome Java video.