[0:00]Now let's try to handle the exceptions. Now we know what is exception, let's try to handle them. So what I will do here is first of all, let's create exceptions, right? That's what then we'll then we'll know how to handle the exception. Now, even before we talk about handling the exception, what type of statements we need to handle? So for that, what I will do is let's talk about statements. See, statements can be of two types. statement can be of a normal statement and a statement can be a critical statement. Okay? See, in real life as well, we try to think about situations which are normal and we try to think about situations which are critical. Example, let's say if you're walking with a kid on the street and or let's say in the mall or in the restaurant, and then, uh, of course, you will grab the hand of kid, right? But what if a kid just runs? In that case, if it's a restaurant, you will not be that panicked, right? You will try to say, okay, it's roaming around and then you will try to catch the kid. But what if the same situation happens on the street, a busy highway where you are holding the hand of a kid, but kid try to run? In that case, you will jump to grab the kid because that situation is critical. In the same way, when you talk about statements, it can be normal statements and critical statements. Example, if I write a simple statement here, which is int I is equal to 9. Now, this statement is normal statement because we know what, what may go wrong here. Everything looks good, right? So this is a normal statement. But what if you say int J is equal to I divide by or maybe I can say 8 divide by I. Now, this statement can be critical, if you look only this statement. Now, why this is critical is because if the value of I is 9, that's completely fine, you will get the output. Example, if I just come back here and if I try to print the value of J, in fact, you know what I will do, I will just make this 18 so that you will get some output instead of zero. Compile and run, this works, right? But what if the value of I is not 9, it's zero? Now, in this case, first of all, let me just clear the screen so that we can see the output properly. And you can see we got an error. It says exception runtime error. Exception in thread main, uh, which is the exception is this, arithmetic exception. So if you can see, it's arithmetic exception and the message is divided by zero. Now, the moment it happens, it actually stops the execution. Let me show you that. If I try to print by here, you will see if I try to run this code and run. You can see it is not printing by. The reason it is not printing by is because at this point, the execution has just stopped. Okay? So that's why it is always better to handle the exception. Even if you know nothing may go wrong, if you think that's critical, handle it. How? That's the question. To handle the exception, what you can do is you can put this type of statements in some other block. Now, first of all, let me just declare the variable here by zero, and then this is a statement which is critical, right? So what I will do is I will just put that in a separate block. So I will put that in a separate block, some something like this. And Now, what I'm saying is, hey Java, just try to execute this code. Okay? If it works, that's fine. Otherwise, execute the other part. Don't stop the execution. The way you can do that, you can say, hey, Java, try to execute this statement. So I can say try. Try to execute. Okay, Java will say, okay, uh, now I know that you are just trying to execute if it's not working, then I will continue with the normal execution here. But then what if Java tries to execute this and there's an error? In that case, it will throw the error. And you as a developer, you have to catch the error. So in this case, you will catch the exception. The moment it throws the exception, it is throwing you an object and you have to accept it as an object. So you will create an object, or you will refer that with an exception class. So this exception here is a class, and you are referring that with any object will do it's not just E, you can say O bj as well. But most of the time you will say E because it makes sense, right? Exception E. You can say O bj as well, that perfectly works, but let me say E here. And if you want to print something, you can do that here. So whatever handling stuff you want to do, you can do that in the catch block. So I will simply say, something went wrong, okay? So what I'm doing is I'm trying to handle the exception, if it works, that's great. If it is not working, it will execute the catch block. Now let's say the value of I is not zero, the value of I is, let's say 4. In this case, you can see there is no exception. So let's try to execute this with the value 4, and if I run this code, you can see it says 4 and bye. There's no problem with the value, we got 4 because of this statement, and we got bye, of course. But then what if something will go wrong? What if the value of I is zero? Now in this case, this will throw the exception. So it will execute the catch block. And I'm expecting it should print something went wrong. Let's try, compile and run. Oh, can you see that? It says something went wrong. Now even if something goes wrong, the execution is still happening. The value of J is zero, because before the execution of this try catch, the value of J was indeed zero. Right? That's how you handle the exception. And again, you don't have to put all the statements here. You can, you can put normal statements in try as well, that completely works, but it's better to put only critical statements inside try block. And you can write multiple try blocks. Uh, so one we have it here. What if you have some few more statements here, you can try catch it as well that that works. So I hope you got the idea what is try. Try will basically try to execute the statements. If everything goes good, it will execute the remaining statement. If something goes wrong, if there's an exception, it will execute the catch block. So point to remember, the catch block will be executed only in case of exception. Otherwise, catch block will be skipped. So that's it from this where we talked about try catch, and we have seen how to handle the exception.
Watch on YouTube
Share
MORE TRANSCRIPTS



