Checked and Unchecked Exceptions Tutorial

When it comes to compiling your source code, it is important to understand the difference between checked and unchecked exceptions. All exceptions are classes that are derived from the Throwable class. Memorizing the class hierarchy is the key to determining whether an exception will be a checked or an unchecked exception. There are two subclasses of the Throwable class, Error and Exception. Error and all of its subclasses down the hierarchy are all unchecked exceptions. Now here is where it gets strange ... all subclasses of Exception are checked exceptions except for RuntimeException and all of its subclasses. RuntimeException and all of its subclasses down the hierarchy are unchecked exceptions.


As the Java compiler is compiling your source code, any statements that invoke methods or constructors that throw unchecked exceptions are given the green light and not required to be enclosed in a try block or specified in the throws list.
Any statements – in your source code – that invoke methods or constructors that throw checked exceptions are stopped at the red light and checked to make sure that they are enclosed in a try block or specified in the throws list.
I haven't gone over how to use the throws keyword just yet, so don't worry about that.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md CheckedUnchecked
C:\Java>cd CheckedUnchecked
C:\Java\CheckedUnchecked>Notepad CheckedUnchecked.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;

class CheckedUnchecked {
    public static void main(String args[]) {       
        uncheckedTester();
        checkedTester();
        System.out.println("Checked and Unchecked exceptions are all about compiling!");
    }

    static void uncheckedTester() {
        int x = Integer.parseInt("3"); // parseInt throws NumberFormatException - derived from RuntimeException - unchecked exception
        //int y = Integer.parseInt("Huh?");
    }

    //static void checkedTester() throws IOException {
    static void checkedTester() {
        File f = new File("c:\\java\\Hello.txt"); // throws NullPointerException - derived from RuntimeException - unchecked exception

        // createNewFile() method throws either IOException or SecurityException
        // SecurityException - derived from RuntimeException - unchecked exception
        // IOException - derived from Exception - checked exception!
        //f.createNewFile();  
    
        /*try {
            f.createNewFile();
        } catch ( IOException e ) {
            System.out.println("Oh no! Not another IOException!");
        } catch ( SecurityException e ) {
            System.out.println("Quick run! The file police are coming!");
        } */
    }
}



Now switch back to the command prompt (CMD) and type in javac CheckedUnchecked.java and press Enter.
Now type in java CheckedUnchecked and press Enter.


C:\Java\CheckedUnchecked>javac CheckedUnchecked.java
C:\Java\CheckedUnchecked>java CheckedUnchecked
Checked and Unchecked exceptions are all about compiling!


Final thoughts

Checked exceptions encourage the developer to consider the implications of invoking a risky method or constructor.


Tutorials