Checked Exception VS Unchecked Exception In Java
In this article, we will discuss checked and unchecked exception in detail with explanation & examples and also list some of the most commonly known checked & unchecked exception.
Checked Exception
- An exception which is checked at compile-time during compilation is known as Checked Exception
- Alternate definition: any line of code that could throw an exception, and if it is raised to handle during compilation is said to be checked exception
- For example, accessing a file from a remote location could throw a file not found an exception
- It is the programmer’s responsibility to handle the checked exception for successful compilation
- This way, if an exception is raised during execution, then respective handling code will be executed
- Note: if it isn’t handled then the program will throw a compile-time error
- Example: IOException, FileNotFoundException, InterruptedException, SQLException, etc
- Except for Runtime exception & its child classes and error & its child classes, all other exceptions fall under the category of Checked Exception
CheckedException.java
import java.io.BufferedReader; import java.io.FileReader; public class CheckedException { public static void main(String[] args) { FileReader fileReader = new FileReader("F:\\BenchRes.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); // logic for reading } }
Output:
Compile-time error: Unhandled exception type FileNotFoundException
Checked exception screen-capture from Eclipse IDE:
Unchecked Exception
- An exception which is NOT checked at compile-time is known as Unchecked Exception
- Alternate definition: any line of code that could throw an exception at runtime is said to be an unchecked exception
- An unchecked exception is because of a programming error
- For example, accessing out of index position to assign some value during execution could throw an exception at runtime
- So, it is again the programmer’s responsibility to handle the unchecked exception by providing an alternate solution in the exception handling code
- Note: if it isn’t handled properly then the program will terminate abnormally at runtime
- Example: Runtime exception & its child classes and error & its child classes are examples of Unchecked Exception
- Like ArithmeticException, NullPointerException, NumberFormatException, ArrayIndexOutOfBoundsException, StatckOverflowError, etc
UncheckedException.java
public class UncheckedException {
public static void main(String[] args) {
char[] ch = new char[4];
ch[7] = 'B';
System.out.println(ch);
}
}
Output:
Exception in thread "main" java.lang. ArrayIndexOutOfBoundsException: 7
at in.bench.resources.exception.handling. UncheckedException.main(UncheckedException.java:8)
Unchecked exception screen-capture from Eclipse IDE:
The misconception about the checked and unchecked exception:
- Sometimes, a checked exception is also referred to as a compile-time exception, and unchecked exception are referred to as runtime exception
- But this is misleading because every exception (whether it is checked or unchecked) occurs/raised only at the runtime, i.e., during program execution-only
- Reason: during compilation; the checked exception is caught and raises a compile-time error, due to which programmer has to handle the exception by providing either try-catch blocks or using throws keyword
- Whereas unchecked exception isn’t caught during compilation, rather it raises an exception during execution because of a programming error
Leave a Reply