Checked or Unchecked Exceptions?
1. Overview
In Java, there are basically two types of exceptions: Checked exceptions and Unchecked exceptions (RuntimeException and Error). The differences between checked, unchecked and error are:
Checked Exceptions must be explicitly caught or propagated. Unchecked exceptions do not have this requirement. They don’t have to be caught or declared thrown.
Checked Exceptions in Java extend the java.lang.Exception class. Unchecked exceptions extend the java.lang.RuntimeException.
Error is an exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction. The unsuccessful read will throw java.io.IOError
. An application might choose to catch this exception, in order to notify the user of the problem — but it also might make sense for the program to print a stack trace and exit.
2. Checked Exception Example
Lets see the code snippets for Checked and Unchecked Exceptions here:
2.1 Create the Checked Exceptions
1 2 3 4 5 |
public class BadURLException extends Exception { public BadURLException(String s) { super(s); } } |
2.2 Create the method which throws Checked Exception and another method that calls it
Here readDataFromURL
is the method which throws Checked Exception BadURLException
and saveDataFromURL
is the caller method. To call readDataFromURL
it has only two choices. Either it catches the BadURLException
or propagates to the call stack.
ThesaveDataFromURL
showed below catches the exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public void saveDataFromURL(String url){ try { String data = readDataFromURL(url); } catch (BadURLException e) { //caught the exception e.printStackTrace(); } } public String readDataFromURL(String url) throws BadURLException{ if(isURLBad(url)){ throw new BadURLException("Bad URL: " + url); } String data = null; //Read data over HTTP and save it as String and return return data; } |
To propagate the exception, change the saveDataFromURL
as below
1 2 3 4 |
public void saveDataFromURL(String url) throws BadURLException{ //propagate the exception String data = readDataFromUrl(url); //not caught } |
3. Unchecked Exception Example
3.1 Create the Unchecked Exception
1 2 3 4 5 |
public class BadURLException extends RuntimeException { public BadURLException(String s) { super(s); } } |
3.2 Create the method which throw RuntimeException
Notice here, readDataFromURL
is not throwing BadURLException
. The saveDataFromURL
method doesn’t have to catch theBadURLException
either.
The saveDataFromURL
method is free from catching the exception (can still choose to catch the exception, but optional) and propagating the exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public void saveDataFromURL(String url){ String data = readDataFromURL(url); } public String readDataFromURL(String url) { if(isURLBad(url)){ throw new BadURLException("Bad URL: " + url); } String data = null; //Read data over HTTP and save it as String and return return data; } |
3. Conclusion
In this post, we had a quick look on Checked and Unchecked exception. Checked Exceptions must be explicitly caught or propagated. Unchecked exceptions do not have this requirement. Which one to choose is the depends on project requirements. But as per Joshua Bloch (Effective Java, item 41: Avoid unnecessary use of checked exceptions)