In Java, there are two types of exceptions:
1) Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.
For example, consider the following Java program that opens file at location “C:\test\a.txt” and prints the first three lines of it. The program doesn’t compile, because the function main() uses FileReader() and FileReader() throws a checked exception FileNotFoundException. It also uses readLine() and close() methods, and these methods also throw checked exception IOException
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown at Main.main(Main.java:5)
To fix the above program, we either need to specify list of exceptions using throws, or we need to use try-catch block. We have used throws in the below program. Since FileNotFoundException is a subclass of IOException, we can just specify IOException in the throws list and make the above program compiler-error-free.
Output: First three lines of file “C:\test\a.txt”
2) Unchecked are the exceptions that are not checked at compiled time. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.
+-----------+ | Throwable | +-----------+ / \ / \ +-------+ +-----------+ | Error | | Exception | +-------+ +-----------+ / | \ / | \ \________/ \______/ \ +------------------+ unchecked checked | RuntimeException | +------------------+ / | | \ \_________________/ unchecked
Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile, because ArithmeticException is an unchecked exception.
Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:5) Java Result: 1
Why two types?
See Unchecked Exceptions — The Controversy for details.
Should we make our exceptions checked or unchecked?
Following is the bottom line from Java documents
If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Exceptions in Java
- Errors V/s Exceptions In Java
- Chained Exceptions in Java
- Built-in Exceptions in Java with examples
- Using throw, catch and instanceof to handle Exceptions in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.reverse() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
Improved By : sakshamcse