In this article, we will discuss checked and unchecked exception in detail with explanation & examples and also list some of most commonly known checked & unchecked exception
1. Checked Exception:
- Exception which are checked at compile-time during compilation is known as Checked Exception
- Alternate definition: any line of code that could possibly throw exception, and if it is raised to handle during compilation is said to be checked exception
- For example, accessing a file from remote location could possibly throw file not found exception
- It is the programmer’s responsibility to handle the checked exception for successful compilation
- This way, if any exception is raised during execution then respective handling code will be executed
- Note: if it isn’t handled then program will throw compile-time error
- Example: IOException, FileNotFoundException, InterruptedException, SQLException, etc
- Except Runtime exception & its child classes and error & its child classes, all other exception falls under the category of Checked Exception
CheckedException.java
package in.bench.resources.exception.handling;
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
1.1 Checked exception screen-capture from Eclipse IDE:
1.2 Possible solution:
- Provide/surround with try-catch block or
- add throws clause in method signature at the end
2. Unchecked Exception:
- Exception which are NOT checked at compile-time is known as Unchecked Exception
- Alternate definition: any line of code that could possibly throw exception at runtime is said to be unchecked exception
- Unchecked exception are because of programming-error
- For example, accessing out of index-position to assign some value during execution could possibly throw exception at runtime
- So, it is again programmer’s responsibility to handle unchecked exception by providing alternate solution in the exception handling code
- Note: if it isn’t handled properly then 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
package in.bench.resources.exception.handling;
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)
2.1 Unchecked exception screen-capture from Eclipse IDE:
2.2 Possible solution:
- First of all, find out what possible Exception/Error program can be thrown during execution
- Then handle those exception either by surrounding with try-catch block or adding throws declaration at the end of method signature, as per business/programming convenience
3. Misconception about checked and unchecked exception:
- Sometimes, checked exception are also referred as compile-time exception and unchecked exception are referred as runtime exception
- But this is mis-leading because every exception (whether it is checked or unchecked) occurs/raised only at the runtime i.e.; during program execution only
- Reason: during compilation; checked exception are caught and raises 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 aren’t caught during compilation, rather it raises exception during execution because of programming error
Related Articles:
- Java – Exception Handling
- Java – Exception Hierarchy
- Java – 5 important keywords in Java Exception handling
- Java – Runtime mechanism, what happens when exception is thrown ?
- Java – Checked Exception v/s Unchecked Exception
- Java – Exception propagation
- Java – try-catch block
- Java – finally block
- Java – try with multiple catch blocks
- Java – Nested try-catch block
- Java – Returning value from method having try-catch-finally blocks
- Java – return statement with finally block
- Java – final v/s finally v/s finalize
- Java – Various methods to print exception information
- Java – throw keyword
- Java – throws keyword
- Java – throw v/s throws
- Java – Difference between throws clause and try-catch-finally block
- Java – Rules for Exception handling w.r.t Method Overriding
- Java – User-defined or Custom exception
- Java – Difference between ClassNotFoundException v/s NoClassDefFoundError
- Java – Top Exception and Error
- Java – Interview question and answers on Exception Handling
- Java 7 – try with resources
- Java 7 – multi-catch block
References:
- https://docs.oracle.com/javase/tutorial/essential/exceptions/
- https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html
- http://www.oracle.com/technetwork/java/effective-exceptions-092345.html
- http://otfried.org/courses/cs206/slides/slides-stackframes.pdf
Happy Coding !!
Happy Learning !!