Difference between Checked and Unchecked Exceptions in Java

There are two types of exceptions - checked and unchecked. The main difference between checked and unchecked exception is that checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.

Checked vs Unchecked Exceptions
Checked Exception Unchecked Exception
Checked exceptions occur at compile time. Unchecked exceptions occur at runtime.
Java compiler checks a checked exception. Java compiler does not check these types of exceptions.
These types of exceptions can be handled at the time of compilation. These types of exceptions cannot be a catch or handle at the time of compilation, because they get generated by the mistakes in the program.
They are the sub-class of the exception class. Here, the JVM needs the exception to catch and handle. They are the sub-class of the RuntimeException class or Error class. Here, the JVM does not require the exception to catch and handle.
Examples of Checked exceptions:
FileNotFoundException,
NoSuchFieldException,
InterruptedException,
NoSuchMethodException,
ClassNotFoundException
Examples of Unchecked Exceptions:
No Such Element Exception,
Undeclared Throwable Exception,
EmptyStackException,
ArithmeticException,
NullPointerException,
ArrayIndexOutofBoundsException,
SecurityException

Exception hierarchy


Throwable /\ / \ / \ / \ Error Exception /\ / \ / \ / \ / \ RuntimeException ClassNotFoundException | IOException | SQLException ArithmeticException NullPointerException

Here, all the classes under RuntimeException and Error are Unchecked Exceptions, while all the classes direcly under Exception class are Checked Exceptions.

Checked exceptions 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.

FileNotFoundException is a checked exception in Java. Anytime, we want to read a file from filesystem, Java forces us to handle error situation where file may not be present in place.

public class Demo {
	public static void main(String[] args) 
	{
		FileReader file = new FileReader("somefile.txt");
	}
}

In above case, you will get compile time error with message – Unhandled exception type FileNotFoundException.

We can reslove this error in two ways either we can propogate this exception further using throws keyword or we can handle this exception using try-catch blocks.

// Method 1: Declare the exception using throws keyword.
public class Test {
    public static void main(String[] args) throws FileNotFoundException
    {
        FileReader file = new FileReader("somefile.txt");
    }
} 
 
// Method 2: Handle the exception using try-catch blocks.
public class Demo {
	public static void main(String[] args) 
	{
		try
		{
			FileReader file = new FileReader("somefile.txt");
		} 
		catch (FileNotFoundException e) 
		{
			//Alternate logic
			e.printStackTrace();
		}
	}
}

Hope you have enjoyed reading Difference between Checked and Unchecked Exceptions in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.