Java Exception Handling Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's Sam welcome back to another Java tutorial where we're gonna be talking about exception handling now this is the chapter in the book that I always used to skip because you don't really need it to get things up and running but if you ever want to work as a professional software developer you need to be able to have proper exception handling in case something in your code goes wrong at 3:00 a.m. you have code that can properly handle that instead of your whole program crashing so what is an exception well simply put an exception is an error that occurs in a program at runtime notice how I said runtime so you can have compile time errors which would be something like something wrong with your syntax this is actually something that goes wrong while your program is executing and these exceptions can be anything from a failed database call to dividing a number by zero the main advantage of exception handling is that it eliminates much of the programmers need to manually check for errors so some older languages like see an error code would be returned when a method fails and then you would have to manually check that which is very tedious and could lead to more errors in Java you just define a block of code called the exception Handler and if any error occurs it will automatically be processed by the exception handler all right so here's a road map of this video first we're gonna look at the exception hierarchy we're gonna look at how to use try and catch which are the two main keywords used when handling exceptions we're gonna look and see what happens if you don't catch an exception we're gonna look at manually throwing exceptions and finally we're gonna look at the keyword finally so the two core components of exception handling are the try and the catch you can't have a catch without a try and vice-versa so this is the format that a general try catch exception handling block looks like so you have your keyword try and then you would have your block of code which is being monitored for any errors if an error occurs it goes into this catch block and then you can write your code for how you want to handle that exception and then finally you have the rest of the program one thing to note is that if everything within the try block goes as expected as in no exceptions happen then nothing within the catch block is executed and the execution of the program resumes after the catch block so a catch block is only executed if an exception is thrown all right so let's look at a basic example of an exception being thrown so let's create an array here with five elements now within this try block let's try and set the tenth element equal to a number like 25 now since this array only has five elements we're trying to index something we're trying to access something that's out of bounds so since the this error is happening within this try block remember the only things I get monitored are within this try block this is gonna throw an exception and let's go ahead and print some stuff out just so we can see what's going on all right so let's go ahead and run this and see what happens so we see that that first try statement gets printed then the exception occurs and then Java stops executing within this try block and goes into the catch statement where it prints out in catch and then finally it resumes execution of the program when it prints out rest of program note that we didn't explicitly tell the program to go into this catch statement Java is pretty smart we got to give it some credit it sees that an exception occurred and it automatically searches for the catch block and runs the code in there following the catch block the program resumes execution so it's the job of your exception handler to remedy the problem that caused the exception so that the program execution can continue normally all right so let's go ahead and change this up here so it doesn't throw an exception and let's see what happens so we see that it prints out in try one then it prints out in try two and then it finally prints out rest of program so notice that it never goes into that catch block now let's see if we gonna trick the program a little bit here let's go ahead and have this error happen let's change it back to throw there let's have it not happen in the main method but let's have it happen in another method so let's create another method here and let's call it error method and it sets the value of index ten to twenty five and then up here we just want to call error method now if we run that we see that it prints out in try one and in fact it does throw the exception because we know it we see that it goes in the catch block and then finally it resumes the rest of the program so even though the error doesn't explicitly happen you know within this main function since this method here has an error it still goes into the catch block and when I'm saying error exception I'm referring to the same thing now one thing that's pretty cool is that you can have multiple catch statements so say you know if there's a divide by zero I want to be able to handle that differently than if we have an array out of bounds exception so Java lets you implement multiple catch statements to do that so in our program let's go ahead and create another catch block and this specific type of exception is going to be array index out of bounds exception and let's go ahead and print something out here so in here we'll have in catch 1 and then let's change this one here to be in catch two let's go ahead and run that and we see that it calls in try 1 the error gets thrown and then we go into that in catch 1 block and then we resume the rest of the program now notice that we still do have this second catch and this here is kind of like a catch-all because you know what if you know we have other stuff in here like a divide by 0 we still want to be able to handle that exception so we can have something that catches specific exceptions and then we can finally have something that's like a catch-all all right so now we're gonna be talking about what happens when an exception is uncaught let's take a look alright so let's go ahead and delete everything here and now say we have we create the array you know we have an error here and say you know this was you know something important that was supposed to happen in your code now let's go ahead and run it and we see down here that an exception gets thrown and the program crashes and say you know say you needed something important to happen that would never happen so I hope you guys can see the importance of having exception handling here so let's look at something a little bit more practical about why you would want to elegantly handle your exceptions so let's create a program that loops through two arrays and divides their numbers so here we have an array denoting the numerators and another one denoting the denominators so let's create a for loop that loops through these alright so we want to create our try catch blocks again so for each iteration of the for loop we're simply going to divide the numerator by the denominator at whatever index were on so as we can see here once we get to this denominator of zero an exception is going to be thrown because we can't divide by zero so the error type that's gonna be thrown is called arithmetic exception and we can print something like you can't divide by zero alright so let's see what happens if we run this so down here we see that the first elements divided without an error the second one as we expected throws that exception and then finally we divide the next two numbers so notice how if we didn't have this try catch block after the second iteration of the for loop an error would be thrown and the last two numbers wouldn't be divided so again exception handling allows us to elegantly catch this exception and recover from it and resume the execution of our program alright so now we're gonna look at how to manually throw an exception so let's go ahead and delete everything except for the try catch so how that would look like is you would just simply type in the word throw and then the type of exception so we could do something like arithmetic exception let's go ahead and read some print statements in here and then the catch block let's go ahead and actually print out the exception so if we hit run we see that a prints out before throw and then it throws that exception which count causes it to go into the catch block and then the catch block prints out the type of exception now you might be asking yourself why would I ever want to manually throw an exception well in Java you can actually create your own exception types that's kind of out of the scope of this video but it's good to know that you can do that and then you can have your program manually throw that type of exception based on if something happens all right so last but not least we're gonna talk about using the keyword finally now sometimes you might want code to run whether or not an exception was thrown you know for example you could have something that throws an exception but you have a file that's open or a network connection that's open you want that connection to close regardless or not of an ik set of whether an exception has been thrown or not so Java provides a convenient way to handle all that using the finally block now to do that what we need to do is we need to include the finally block at the end of a try-catch sequence the general form of a try-catch that has a finally as shown here so we you know we have our normal try block we have a couple of catches and then down here we have that finally keyword with a block of code with you know whatever you would want to be in there so let's take a look at an example here so let's just keep this simple and only have one catch block all right so let's go ahead and recreate that that array index out of bounds error all right so if we go ahead and run that we see that we are in the try block the exception happens it prints out an exception and then it calls that finally block so you know like I said if you have like some kind of file connection open you're gonna want to close it regardless of whether there was an exception or not now here's something else that's kind of interesting so let's go ahead and have this go into the catch block again but let's try and return out of this statement so once it goes to the catch it calls returned so is it gonna go still go in this finally block what do you guys think well let's go ahead and run it and see we see that it still goes into that finally block even though we've actually explicitly told the program to return after the exception was called it will call that finally block regardless all right guys that's all I wanted to cover in this video hopefully you guys found it helpful if you did please you know appreciate it if you hit that like button and yeah thank you guys so much for watching and as always keep on coding [Music] [Music] [Music]
Info
Channel: Keep On Coding
Views: 69,612
Rating: undefined out of 5
Keywords: java exception handling, java exceptions, exception handling in java, java exception handling tutorial
Id: sQwTGB6gW-8
Channel Id: undefined
Length: 11min 38sec (698 seconds)
Published: Wed Jul 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.