 Hello friends, I am Sanjay Gupta in this video I am going to demonstrate you how you can implement divide by zero exception in Java. Before starting, 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 Techimace that is available on Google Play. You can connect with me by following these details which are available in front of you. Now coming onto the topic, you can see a piece of code is available in front of you. This code contains an exception. I will be demonstrating this, that exception while I will be executing this code. First I am going to explain each statement which are available in this code. So first statement is a comment, an application that attempts to divide by zero. So this complete code doesn't contain any exception handling right now. When I will be showing an error message then I will show how you can convert or you can implement the concept of exception handling in this code. Second statement is importing scanner class which is available in Java.util package. Then public class test is defined here which is containing two methods. First one is quotient and second method is main. Quotient method is receiving two arguments n and d and for numerator d for denominator and it is returning the result of n by d. Return type of quotient method is int and its nature is static. So static methods are called without creation of any object. Then second method main is defined. Inside main scanner class reference is created. Then with the help of system.out.println please enter an integer numerator will be displayed. Then that numerator value is read by next.int method and the result will be stored inside n. Then system.out.print will display enter please enter an integer denominator on screen. Then entered value will be stored inside d with the help of next int method. Here at line number 14 quotient method is called which is passing numerator and denominator values. Result will be stored inside result variable that will be displayed by system.out.println and then finally by message will be displayed as output. So this is the explanation of the code. Now I am executing this code so that you can check how exceptions can be displayed by the code. So I am entering 100 as numerator and I am entering 50 as denominator. So you can see that result equals to 2 is displayed and by is also displayed as output. Now I am hearing this output and I am again executing the code. It is asking again numerator value. This time I am entering 100 as numerator and 0 as denominator. You can see an exception method has been displayed here and as exception is generated code is terminated and nothing has been printed which are available after that exception condition. So this is the problem of exception whenever exception occurs in the code at that point of time a program is terminated and nothing will be executed which are available just after that exception. It means exception has occurred at the calling of this method quotient. So if exception is occurred here then remaining instruction will be will remain un-executed because of exception. So if you want to execute remaining instructions whether exception is occurring or not then you have to implement exception handling mechanism in your program. So now I am going to demonstrate you how you can implement exception handling in your code. For that purpose you have to use two keywords that are try and catch. So first I am using try keyword. You can see that I have put two statements inside try keyword or try block. First is calling of quotient method and second is system.out.println which will display the result. These two statements are interdependent if exception is occurring in quotient method calling on method definition then this print statement should not be executed. That's why I have put these two statements inside try block. Now if any exception is occurring then also last statement by will be displayed. So I have demonstrated the concept of exception handling without try and catch. You have seen that when exception has occurred nothing is displayed even by is not printed but now I am going to handle this exception then you will see that the masses by will be displayed even after exception is occurring in the code. Now I am implementing catch. So this is the modified code which is having try and catch statements or you can say block in the code. If exception is occurring then control will be transferred from try to catch automatically catch will show this message on screen or console and then control will be transferred to the statements which are available just after the catch lock. And if no exception is available then all the statements will be executed properly. Now I am saving this code and I am executing the code. First I am entering 100 and then 50. You can see result and by both has been displayed on the screen. I am clearing the console and I have executed the code again. This time I am entering 100 as numerator and 0 as denominator. You can see that no error message has been displayed. Division by 0 is printed because I have used try and catch and inside catch I have implemented system.out.println and inside that println method I have implemented a text division by 0 which is displayed here and by is also displayed which is written after the catch statement. So friends I hope you have understood the concept of exception handling well now with the help of try and catch we can guard some set of instructions which are generating or which are raising any exception in the code and those raised exceptions can be handled by catch block and a proper message can be displayed for the user that will be an error message and remaining statements which are available just after the try or catch block will be executed properly. So this way you can handle some set of instructions and your all statements will be executed well even exception is available in the code or not. So implement this example in your system so that you can understand how you can prevent or you can implement exception handling with the help of try and catch keywords in Java. If you want to watch more programming related videos you can subscribe my youtube channel thank you for watching this video.