Last Updated: 30 September, 2023
An exception is an unexpected event that occurs during program execution and affects the current flow of the program execution, which can cause the program to terminate abnormally.
In Java, exceptions have been categorized into two types, and they are as follows:
✅ Checked Exception
✅ Unchecked Exception
A checked exception is one that the compiler examines during the Java program's compilation process to determine whether the programmer has handled it or not. If it is not handled, the compiler displays a compilation error using built-in classes.
The checked exception is also known as a compile-time exception.
In Java, checked exceptions are handled using either try-catch blocks or a throws keyword in the method declaration.
There are some built-in classes used to handle checked exceptions in Java:
All the checked exception classes are direct child classes of the Exception class.
Let's look at an example of a checked exception below.
Checked Exception Example
Output
java.io.FileNotFoundException: C:\javabytechie\java\examples\StudentReport.txt (The system cannot find the path specified)
An unchecked exception is an exception that occurs at the time of program execution. Unchecked exceptions are typically the result of bugs like logic mistakes, improper resource usage, etc. At the time of compilation, the compiler does not catch the unchecked exceptions.
The unchecked exception is also known as a runtime exception.
There are some built-in classes used to handle unchecked exceptions in Java:
All the unchecked exception classes are children of the RuntimeException class, which is a child class of the Exception class.
Let's look at an example of a checked exception below.
Unchecked Exception Example
Output
Exception in thread "main" java.lang.NullPointerException at com.javabytechie.exceptionhandling.UncheckedExceptionExample.main(UncheckedExceptionExample.java:9)
Checked Exception | Unchecked Exception |
Checked and handled at the time of the compilation of the program. | Not checked and handled at the compile time, but occurs at the run time of the program. |
A checked exception is also known as a compile-time exception. | Unchecked exceptions are also known as run-time exceptions. |
A checked exception occurs when the chances of failure are too high. | Unchecked exceptions occur mostly due to programming mistakes. |
When a method throws a checked exception and the compiler cannot handle it on its own, the program gives a compilation error. | The program compiles fine because the exceptions escape the notice of the compiler. Exceptions occur due to errors in the programming logic. |
They are subclasses of the exception class, but they do not inherit the Runtime Exception class. | They are subclasses of the Runtime Exception class. |
JVM enforces developers to handle or catch it. | JVM does not require developers to handle or catch it. |
There are many exceptions that fall under checked exceptions, such as
| Unchecked exceptions include
|
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com