What are checked and unchecked exceptions? They are two types of exceptions in Java. A check exception is an exception that must be handled by a program; if it is not handled, the program will abort. An unchecked exception is an exception that does not have to be handled; if it is not handled, the program will continue execution with whatever happened to the object being thrown. Unchecked exceptions are usually programming errors. Let’s take a closer look at these two types of exceptions.
What is a Checked Exception?
Checked Exception are those that the programmer must deal with. They are subclasses of Exception. Checked Exceptions are checked at compile-time. Checked Exception must be handled by the program or else the program will not compile. Checked Exception includes IOException and SQLException etc.
- Checked Exception forces the programmer to either handle them using try-catch blocks or declare them using the throws keyword. Checked Exception should not be ignored at compile time as they might occur at runtime. Checked Exception represents some recoverable conditions and hence they are checked by the compiler.
- Checked Exceptions are known to the programmer as they might occur while he is writing the code. For example, FileNotFoundException is a checked exception and it occurs when trying to access a non-existent file. The compiler forces the programmer to either catch this exception or declare it using the throws keyword so that the caller of the method knows about this exception.
- Checked Exceptions are also called Compile Time Exception. There is no point in handling an unchecked exception as it represents an unrecoverable condition like OutOfMemoryError, StackOverflowError, etc which occur due to programming errors or incorrect use of API.
Asides from these two types of exceptions, there is another type of exception known as Error which represents irrecoverable system errors like virtual machine errors, hardware failures, etc. We should not try to handle Error as it represents serious problems in our application like JVM crash and we should let our system deal with them.
What is an Unchecked Exception?
Unchecked exceptions are those which extend the RuntimeException class. These include programming bugs, such as logical errors or improper use of an API. Unchecked exceptions do not need to be declared in a method or constructor’s throws clause if they can be handled locally, but it is considered good practice to do so.
When an unchecked exception is thrown, the program will terminate and print an error message. This can be frustrating for users, so it is important to fix any bugs that may cause an unchecked exception to be thrown. In general, unchecked exceptions should be avoided, as they can lead to unexpected behavior and make it difficult to debug a program.
Difference between Checked and Unchecked Exception
Checked and unchecked exceptions are two types of error conditions that can occur in a Java program.
- Checked exceptions are typically caused by external factors, such as a file not being found or a network connection being unavailable.
- These types of exceptions must be handled by the program, either by catching them or declaring them in the throws clause of a method.
- Unchecked exceptions, on the other hand, are usually due to programming errors, such as an index out of bounds or a null pointer dereference.
- These types of exceptions do not need to be explicitly handled but can be caught if desired.
However, unchecked exceptions will propagate up the call stack until they are either caught or cause the program to terminate.
Conclusion
Java provides two exception-handling mechanisms- checked and unchecked exceptions. The main difference between these two types of exceptions is that the compiler checks for errors during compilation if you use checked exceptions, while it does not do so for unchecked exceptions.
Another key difference is that the compiler forces you to deal with a checked exception either by catching it or declaring it in your method’s throws clause. Unchecked exceptions are automatically wrapped in a RuntimeException and thus don’t have this safety net. You should generally use unchecked exceptions only when you know there is no way the user can recover from the error.