Checked vs. Unchecked Exceptions in Java Tutorial - What's The Difference?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you've probably heard people talking about checked and unchecked exceptions in java but what does that mean what exactly is the difference between a checked and an unchecked exception and what do you need to know about how to handle each of those types of exceptions in your java programs we'll cover all of that with some concrete coding examples in this video my name is john i'm a lead java software engineer and i love sharing what i've learned with you in a clear and understandable way i also have a full java course available and a link down in the description if you're interested let's get to it so first what exactly is the difference between a checked and an unchecked exception well a checked exception is one that is checked for at compile time but what exactly does that mean well let's talk about it with an example let's say we have a method in our program where we want to just read from a file so one way we could do that is private static void read file and it takes in a string called file name and one way to read from a file is using a file reader call it reader equals new file reader and that takes in the file name that we want to open and we can call this read file method from our main method here just by doing a read file and passing in the name of the file that we want let's just say it's called my file dot txt but as you can see here eclipse is giving us an error it says unhandled exception type file not found exception so what's happening here is when we try to create this file reader it could throw an exception if the name of the file that we pass in doesn't actually exist it could throw a file not found exception and java is giving us an error here and telling us that we need to deal with the possibility of this file not found exception happening when this piece of code is called and that is because a file not found exception is a checked exception checked exceptions are exceptions that java makes you deal with one way or another at compile time that means that before your program will even compile successfully java checks to ensure that you're dealing with the possibility of that exception happening and it won't compile until you do so right now if we try to run this program we can't because we still have a compilation error this error is blocking us from even compiling our code if the file not found exception was an unchecked exception we wouldn't be seeing this error here we'll talk more about unchecked exceptions a bit later in the video so how do we deal with this error that we get for checked exceptions well we can do it in one of two ways the first way is that you can surround this line of code that throws the exception with a try catch block so to do that you would just write a try and then inside your try block you have the line of code that could throw the exception in this case the file not found exception from this piece of code here then you have it catch a file not found exception called fnfe for file not found exception inside this catch block you just want to put whatever code you'd like to run if this exception happens to get thrown so in the case of a file not found maybe you just want to print something out to the user that says hey that file doesn't exist if we run our program instead of seeing an exception and a stack trace we see hey that file does not exist the exception was caught here in the catch block and all it did was print out that message instead of blowing up the entire program the other way you can deal with that error is instead of having a try catch you can just tell java yep i totally understand that this piece of code that i'm calling could throw an exception but i'm not going to deal with it so i'm just going to declare to you java that i could throw it and whatever piece of code that calls this method they're just going to have to deal with that exception themselves and to do that you just put a throws declaration right here in the method signature right before the curly braces so we'll say throws file not found exception all right so great now we no longer have that error here in our code that we had before but now we actually have an error here where we're calling our read file method and it's the exact same error unhandled exception type file not found exception what happened is that now if that file not found exception is thrown there's nothing in this method to catch that exception so that exception will just get thrown from this method out to whatever piece of code called that method since this line calls that method now this line could throw a file not found exception and since it's a checked exception still that means we have to deal with it here so if i try to run the program now i can't because i still have a compilation error here and we can deal with the possibility of that exception being thrown here the same way that we could down here we could surround the statement with a try catch or if we don't want to do that we can just declare that we throw this exception in the method signature by adding throws file not found exception now you'll notice that putting that throws declaration here doesn't cause another error like it did when we put the throws declaration down here and that's only because this method is the main method at the very top of our program once that exception gets thrown up past your main method your program just dies right there immediately and all you'll see is a stack trace output for that exception all we did was tell java hey i'm going to throw that exception we didn't have anything that actually dealt with it so our program still dies so eventually somewhere in your method hierarchy you do want to have a try catch that catches that exception and deals with it properly and you can check out this video here for a much more in-depth tutorial about how to use try catch now on to unchecked exceptions as we know if you have some code that could throw a checked exception you have to deal with it either by using a try catch block or declaring that you throw it in your method signature that is not the case for unchecked exceptions so what does it look like with unchecked exceptions let's say we just had a method like this one here that just takes in a string we'll call it my string and all it does is just print out the length of that string so it prints out my string dot length so up here we could declare a string let's call it a name and set it equal to john and then we can call that print length method and pass in name and of course it prints out four because the length of john is four characters but what would happen if instead of initializing this name string to john we set it equal to null so of course what happens is we end up passing in null as the parameter to this print length method then when it tries to take that parameter and call the length method on it it's going to get a null pointer exception because this my string will be null and if we run it of course we do see that it gets a null pointer exception so obviously we have a case here where this method could throw a null pointer exception right but java doesn't give us any compilation errors here it compiles fine and then only explodes when we actually run it and that is all because null pointer exception is an unchecked exception java doesn't make us catch it with a try catch block and it doesn't even make us declare it here in the method signature that we might throw a null pointer exception that's the main difference between checked and unchecked exceptions that doesn't mean you can't do those things though if you want to you can absolutely put a throws null pointer exception here it just doesn't really matter whether you do or not and you also absolutely can add a try catch around this piece of code and you probably should because if you don't that exception is going to get thrown and your program is going to die just like it did for us here so we put that in our try block and we can catch null pointer exception npe and just print out string cannot be null and now if we run our program again we don't get any exception we just get a message to the user that says the string cannot be null here's another big question though how does java determine which exceptions it treats as checked exceptions and which it treats as unchecked exceptions here's an image from how to do in java.com that shows java's exception hierarchy java's exception class is a subclass of throwable error is the other subclass of throwable errors are separate from exceptions they're usually for things that can't be recovered from like out of memory error stack overflow error things like that so it's a bit outside the scope of this video anyway all exceptions in java are subclasses of the exception class one subclass of exception is runtime exception and there are many subclasses of runtime exception like null pointer exception that we saw in this video number format exception and many many others in java the runtime exception and all subclasses of runtime exception are unchecked exceptions and any exception that isn't a runtime exception or a subclass of runtime exception is a checked exception and that includes the exception class itself and any other subclasses of exception besides runtimeexception that includes the file not found exception that we saw in this program and tons of others so runtime exceptions and all of its subclasses are unchecked exceptions and every other type of exception is a checked exception this will illustrate the difference pretty clearly if inside this method we just flat out throw a new exception we get that compiledtime error unhandled exception type exception and we have to deal with it for our program to compile but if instead of throwing an exception we throw a runtime exception now we don't have any compilation errors at all and that's because exception is a checked exception and runtime exception is an unchecked exception now again you should deal with unchecked exceptions in your code somewhere along the line with a try catch just like you should for checked exceptions the difference is just that java doesn't check to make sure that you're dealing with them at compile time for unchecked exceptions but it does do that check for checked exceptions if you enjoyed this video or learned something please let me know by hitting the like button and be sure to subscribe so you don't miss each week's new java tutorial and don't stop now continue your java learning by checking out more videos like one of these below thank you so much for watching and being here with me i really do appreciate it i'll see you next time
Info
Channel: Coding with John
Views: 104,219
Rating: undefined out of 5
Keywords: java, codingwithjohn, coding with john, java beginner lesson, java checked vs unchecked exceptions, checked vs unchecked exceptions java, checked vs unchecked exception in java example, java unchecked exception vs checked exception, java checked and unchecked exceptions, learn java, java tutorial for beginners, unchecked exceptions, checked exceptions, java checked exceptions, unchecked exception java
Id: bCPClyGsVhc
Channel Id: undefined
Length: 10min 14sec (614 seconds)
Published: Mon Nov 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.