Java Checked And Unchecked Exceptions

In this tutorial we will examine the difference between Java checked exceptions and Java unchecked exceptions also known as runtime exceptions.

In Java there are two main categories of exceptions: Checked exceptions and unchecked exceptions (Runtime Exceptions). More modern languages such as Kotlin, Scala, C# have no checked exceptions, only runtime exceptions. Other languages such as Go have no exceptions.

When we look at the Java Exception Hierarchy we can see that checked exceptions extend from the Exception class while unchecked exceptions extend from the class RuntimeException which also extends from the Exception class. There are different arguments about whether we should use checked or unchecked exceptions within our code. Before looking at some of these arguments lets quickly review both types of java exceptions.

Checked Exceptions

Java Checked exceptions are 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 declare in the java method signature that the method throws an exception by listing it in the throws clause. For example, consider the following code that opens file to read its contents.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
private static void readFile() throws IOException {
    FileInputStream is = null;
    try {
        is = new FileInputStream(ResourceUtils.getFileFromResources("file.txt"));
        int data = is.read();
        while (data != -1) {
            //read the contents
        }
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

The above code throws a FileNotFoundException at line 4, and an IOException at lines 5 and 11. If we don’t handle these exceptions in the code or declare that this method throws these exceptions in the throws clause the code will not compile.

Java Unchecked Exceptions

Firstly it is important to say that Unchecked or Runtime exceptions are functionally equivalent to java checked exceptions. The main difference between checked and unchecked exceptions is that we do not have to explicitly handle unchecked exceptions within our code unless we decide to.

1
2
3
4
5
public void divideByZero() {
    int x = 10;
    int y = 0;
    int result = x / y;
}

For example if we execute the above method it will result in a java.lang.ArithmeticException because dividing by zero is not allowed. Since ArithmeticException is a RuntimeException we do not have to explicitly handle it within our code.

1
2
3
4
public static void main(String[] args) {
    Exceptions exceptions = new Exceptions();
    exceptions.divideByZero();
}

If we run the above code without explicitly catching the exception our program will fail with a stack trace such as:

1
2
3
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.stephenenright.tutorials.corejavaexception.exception.Exceptions.divideByZero(Exceptions.java:8)
    at com.stephenenright.tutorials.corejavaexception.exception.Exceptions.main(Exceptions.java:15)

So this is the main difference between java checked and runtime exceptions is that the compiler enforces us to handle java checked exceptions but does not enforce us to handle runtime exceptions.

Checked Exceptions Versus Unchecked Exceptions

When should you use a checked exception or an unchecked exception, well Java checked exceptions are a very controversial language feature with some programmers for them and others against them. So why should you choose one over the other. Well, firstly I have to say is that you should follow the coding conventions within your team, and always do what you feel is correct in a given situation. It is common practice in Java to use checked exceptions for cases that you can recover from and unchecked (runtime) exceptions for cases that you should not such as programming errors etc. This makes perfect sense and it was the way the Java language was designed. However, there are some disadvantages to using checked exceptions, and as program languages evolved some languages have decided not to facilitate checked exceptions.

Advantages Of Checked Exceptions

Some of the advantages of Java checked exceptions include:

  • Checked exceptions are enforced by the compiler enforcing them to be handled.
  • Unlike unchecked exceptions checked exception become part of the API so its clear what exceptions are thrown.

Advantages Of Unchecked Exceptions

Some of the advantages of Java unchecked exceptions include:

  • Unchecked exceptions reduce the amount of boilerplate code you need to write and the amount of code you need to write.
  • Checked exceptions force you to explicitly declare the exceptions a method throws. This approached makes it harder to modify the method signature, by adding, removing exceptions as it requires all methods higher in the call stack to be updated. This makes unchecked exceptions more maintainable.
  • Checked exceptions force them to be always handled in this case inexperienced developers may write less than ideal exception handling code.
  • Checked exceptions make it harder to write functional code that is composed of different functions.

Conclusion

In this short tutorial we looked at the difference between checked and unchecked exceptions in Java. We also looked at some of the advantages and disadvantages of them.