 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can use finally keyword in Java. Before starting, you can note my information. You can subscribe my YouTube channel by clicking on the link which is available at bottom right corner of this video. You can download my app Takimis that is available on Google Play. You can connect with me by following these details which are available in front of you. Now coming on to the topic, first I am going to explain the concept of finally block. Then I will be explaining a program which is written in Java that will explain you the use of finally block in exception handling in Java. So you can read the first point. The finally block is optional and is sometimes referred to as the finally clause. So this finally block is optional. You don't have to use this block every time in the program whenever you are implementing exception handling. If you require this block, then you can use otherwise you can avoid it. If it is present, it is placed after the last catch-off. As you might know, we can implement multiple catch blocks for a try block in exception handling in Java. So if you are implementing multiple catch blocks, then after the last block, after the last catch block, you have to place finally clause. Third point says Java guarantees that a finally block will execute whether or not an exception is thrown in the corresponding code, sorry, in the corresponding try block or any of its corresponding catch block. So this is the use of try block. Whether the exception is raised or not, finally block will be executed every time. Java also guarantees that a finally block will execute if a try block exits by using a return break or continue statement. So if control will be transferred from try to another block using these keywords that are return break or continue, then also finally block will be executed. Last point says the finally block will not execute if the application exits early from a try block by calling the method system dot exit. So if system dot exit method is called inside try block, then finally block will not be executed. So this is all about finally block. Now with the help of an example, we can understand the working of finally you can see a code which is written in front of you name of classes test. It is having two methods. First one is quotient and second one is main. Inside main method, I have declared two variables 100 and sorry and and D and is containing 100 and D is containing zero. Inside try block quotient method is called, which is receiving two arguments and and D in the definition of quotient. It is returning and upon D. So numerator value will be divided by denominator and the resultant value will be returned and return type of this method is int. It is static by nature. So we can call this method without creating any object and the result will be stored inside this result variable. This system dot out of println will display the result which is received by the result variable. Then after try catch block is defined catch will display divide division by zero if any exception will be raised and after catch finally block is available. Whatever you will write in the inside this finally block will be executed every time whether the whether the exception is raised or not. So suppose if you are dealing with file handling then after completion of the code you must have to close the file. If in that code an exception is raised and file is not closed by the try block then that statement will or that statement can be written inside finally block so that if any exception is raised then also all the files that are opened in try block will be closed inside finally block. So here I have only mentioned finally block executed for a demonstration purpose. So right now you can see numerator is 100 and I am assigning denominator 50. It means no exception will be raised. So let's see whether finally block will be executed or not. So I am showing console screen and I am executing this code. You can see the output result 100 upon 50 equals to 2 is displayed with the help of this statement which is available at line number 9 and you can see finally block executed it is displayed because of finally clause. Now suppose denominator value is 0 and if I execute this code you can see divide by 0. This is displayed with the help of this catch block and catch is containing system dot out dot print lm at line number 12. So here an exception is raised. So control is transferred from try to catch and catch is displaying this message then after execution of catch control is transferring to finally and finally is printing finally block executed. So friends you have seen that whether the exception is raised or not finally block is executing every time whenever you are running your code. So this finally is optional. If you remove this finally block then nothing will be executed which are available inside finally block and if you place this finally block in your code then all the statements which are available inside it will execute whether the exception is available or not. So you can do this in your system so that you can understand the concept of finally clause. If you want to watch more programming related videos you can subscribe my YouTube channel. Thank you for watching this video.