There are three types of exceptions in java.
- Checked Exception / Compile Time Exception
- Unchecked Exception / Runtime Exception
- Error

Checked, Unchecked, Error are raised only on Runtime. They never raised at compile time.
Checked Exceptions / Compile Time Exception
Throwable, Exception and all their sub classes (except RuntimeException, Error and their subclasses) put together are known as Checked Exceptions.
Checked Exception is an exception which is checked by the compiler during the compilation time and hence it is also called as Compile Time Exception.
The Compiler follows “Handle Or Declare Rule” for the checked exceptions, if a code throws compile time exception the compiler demand us to handle the exception with try-catch or declare the exception in the method using the throws keyword. Otherwise we will get compilation error. Let us try to understand with the following Example program.
Example : checked exception – checked by compiler – compilation error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.vidvaan.corejava.exception13.typesOfExceptions; import java.io.FileInputStream; // checked exception - checked by compiler - compilation error public class CheckedExceptionExample { public static void main(String[] args) { System.out.println("main begin"); FileInputStream fis = null; // constructor FileInputStream(File filename) throws FileNotFoundException, // FileNotFoundException is a checked exception fis = new FileInputStream("demo.txt"); int c; // Method FileInputStream.read() throws a checked exception:IOException while ((c = fis.read()) != -1) { System.out.print((char) c); } // Method FileInputStream.close() throws a checked exception:IOException fis.close(); System.out.println("main end"); } } |
Compilation Errors :
CheckedExceptionExample.java:14: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
fis = new FileInputStream(“demo.txt”);
^
CheckedExceptionExample.java:18: error: unreported exception IOException; must be caught or declared to be thrown
while ((c = fis.read()) != -1) {
^
CheckedExceptionExample.java:23: error: unreported exception IOException; must be caught or declared to be thrown
fis.close();
^
3 errors
In the above example, We are reading the file demo.txt
and displaying its content on the console. We got three compilation errors as we mentioned in the comments of the program. We can handle checked exception with try-catch block or declare the exception with throws keyword.
Some of the common checked exceptions are …
- IOException
- SQLException
- FileNotFoundException
- ClassNotFoundException
- MalformedURLException
- InvocationTargetException
- ParseException
- InterruptedException
- MalformedURLException
Unchecked Exception / Runtime Exception
RuntimeException and all its sub classes put together are known as Unchecked Exceptions.
Unchecked Exception is an exception which is not checked by the compiler hence it is also called as Runtime Exception.
If your program is throwing an unchecked exception, Even if you didn’t handle/declare that exception, the program won’t give compilation error.
Example : unchecked exception – not checked by compiler – no compilation error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.vidvaan.corejava.exception13.typesOfExceptions; // unchecked exception - not checked by compiler - no compilation error public class UncheckedExceptionExample { public static void main(String[] args) { System.out.println("main begin"); int num1 = 1;// may get data from user int num2 = 0;// may get data from user // Since I'm dividing an integer with 0 it should throw unchecked // ArithmeticException // But Compiler is not demanding us to handle the exception here int res = num1 / num2; System.out.println(res); System.out.println("main end"); } } |
Output :
main begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception13.typesOfExceptions.UncheckedExceptionExample.main(UncheckedExceptionExample.java:14)
In the above example, We are not getting any compilation error even there is a chance of getting unchecked ArithmenticException. So It clear that unchecked exceptions are not checked by the compiler. We can handle unchecked exceptions with try-catch or pre-conditions.
Some of the common unchecked exceptions are …
- NullPointerException
- ArithmeticException
- NumberFormatException
- ArrayIndexOutOfBoundsException
- StringIndexOutOfBoundsException
- IllegalArgumentException
- IllegalStateException
- ClassCastException
Error
Error and all its sub classes put together are known as Errors. Errors are irrecoverable problems. Errors occur due to lack of system resources like Memory Issues. We never handle and can’t handle and never catch Errors. If Errors are raised in the application execution it will simply get terminated.
Errors also comes under unchecked exceptions, But Errors represents irrecoverable problems. Let’s see an example of Error below which leads StackOverFlowError.
Example : Error – irrecoverable problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.vidvaan.corejava.exception13.typesOfExceptions; // Error - irrecoverable problem public class ErrorExample { public static void main(String[] args) { call();// method calling } public static void call() { call();// recursive method calling } } |
We can handle Errors by upgrading system resources as per application requirement. But we can’t avoid termination of the application when Errors raised.
Example Errors are shown below.
- OutOfMemoryError
- VirtualMachineError
- AssertionError
- IOError
- AnnotationFormatError
- StackOverFlowError