Java Fundamentals

Java Fundamentals Checked & Unchecked Exceptions

Learning Paths:
Check out a free preview of the full Java Fundamentals course:
The "Checked & Unchecked Exceptions" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses the two categories of exceptions, checked and unchecked, and the differences between the two. Check exceptions are exceptions that are checked by the compiler, while unchecked exceptions are not checked by the compiler.

Get Unlimited Access Now

Transcript from the "Checked & Unchecked Exceptions" Lesson

>> So there are two categories of exceptions. We have checked and we have unchecked exceptions. So the signature of the createNewFile method, it specifies. This is what it looks like. It specifies that it throws an IOException. It says it right in its signature. And so when we tried to say file.createNewFile, that's why we got an error, because it already told us in its signature that there's a possibility of actually throwing an exception.

[00:00:37] So that's how the compiler knew, hey, there's a chance, right? So we have to put that within a try block. So that's a check, an example of a checked exception. But remember when we looked at the for loop example, and I said this will compile, there's no anything here that's stopping me from saying do it while index is less than 4.

[00:01:04] That's an unchecked exception, right? So in this example, the nextDouble method, for example, on scanner, it does not specify that it throws any exception. And yet if I were to say console.nextDouble, and somebody entered a string, then I would get this InputMismatchException. So this is an unchecked exception.

[00:01:35] With unchecked exceptions, I don't have to put these in try blocks. I might not even know, because the method signature didn't tell me that there's a possibility of it throwing an exception. So I wouldn't even know to put it in a try block or which exception I should even catch.

[00:01:52] So checked exceptions are the exceptions that are checked by the compiler before the code is executed. If the method says that it throws a checked exception, then it must declare that. If it does it in the method, it must declare it in its signature. And this is the act that lets the compiler know that any code that's gonna call this method has to handle that exception in some way, all right?

[00:02:21] For unchecked exceptions, these are not checked by the compiler. These are runtime exceptions, runtime meaning when it's executed. And there's actually a class in Java that's called runtime exception. So runtime exception in all of its subclasses are considered unchecked exceptions. These exceptions can be thrown by a method, but the method header does not need to explicitly specify that it's going to throw one of those exceptions.

[00:03:00] Is that clear? All right, so methods typically use checked exceptions when it's reasonable that a calling method can recover from the error, right? So, for example, with that IOException, If I caught that in my try-catch block, there's something I could do as a programmer. I could create the directory and then try the action again, right?

[00:03:29] So there's things that I can do. Versus unchecked exceptions are used when there really isn't anything that you can do to recover from this, it's best it just blow up. Now, while the unchecked exceptions are not required to be handled, your code can still specify them within the catch block and do whatever you want.