Types of Exceptions

Man using computer
BJI/Blue Jean Images/Getty Images

Errors are the bane of users and programmers alike. Developers obviously don't want their programs falling over at every turn and users are now so used to having errors in programs that they grudgingly accept to pay the price for software that will almost certainly have at least one error in it. Java is designed to give the programmer a sporting chance in designing an error-free application. There are exceptions that the programmer will know are a possibility when an application interacts with a resource or a user and these exceptions can be handled.

Unfortunately there are exceptions the programmer can't control or simply overlooks. In short all exceptions are not created equal and therefore there are several types for a programmer to think about.

What Is an Exception? takes a closer look at what the definition is and how Java handles them but suffice to say, an exception is an event which causes the program to be unable to flow in its intended execution. There are three types of exception - the checked exception, the error and the runtime exception.

The Checked Exception

Checked exceptions are exceptions that a Java application should be able to cope with. For example, If an application reads data from a file it should be able to handle the FileNotFoundException. After all, there is no guarantee that the expected file is going to be where it is supposed to be. Anything could happen on the file system which an application would have no clue about.

To take this example one step further. Let's say we are using the FileReader class to read a character file. If you have a look at the FileReader constructor definition in the Java api you will see it's method signature:

public FileReader(String fileName)
  throws FileNotFoundException

As you can see the constructor specifically states that the FileReader constructor can throw a FileNotFoundException.

This makes sense as it's highly likely that the fileName String will be wrong from time to time. Look at the following code:

    public static void main(String[] args){
        FileReader fileInput = null;
        
        //Open the input file
        fileInput = new FileReader("Untitled.txt");
    }

Syntactically the statements are correct but this code will never compile. The compiler knows the FileReader constructor can throw a FileNotFoundException and it's up to the calling code to handle this exception. There are two choices - firstly we can pass the exception on from our method by specifying a throws clause too:

    public static void main(String[] args) throws FileNotFoundException{
        FileReader fileInput = null;
        
        //Open the input file
        fileInput = new FileReader("Untitled.txt");
    }

Or we can actually handle with the exception:

    public static void main(String[] args){
        FileReader fileInput = null;
        
        try
        {
           //Open the input file
           fileInput = new FileReader("Untitled.txt");
        }
        catch(FileNotFoundException ex)
        {
            //tell the user to go and find the file
        }
    }

Well-written Java applications should be able to cope with checked exceptions.

Errors

The second kind of exception is known as the error. When an exception occurs the JVM will create an exception object. These objects all derive from the Throwable class. The Throwable class has two main subclasses - Error and Exception. The Error class denotes an exception that an application is not likely to be able to deal with. 

These exceptions are considered rare. For example, the JVM might run out of resources due to the hardware not being able to cope with all the processes it is having to deal with. It's possible for the application to catch the error to notify the user but typically the application is going to have to close until the underlying problem is dealt with.

Runtime Exceptions

A runtime exception occurs simply because the programmer has made a mistake.

You've written the code, it all looks good to the compiler and when you go to run the code it falls over because it tried to access an element of an array that does not exist or a logic error caused a method to be called with a null value. Or any number of mistakes a programmer can make. But that's okay, we spot these exceptions by exhaustive testing, right?

Errors and Runtime Exceptions fall into the category of unchecked exceptions.

Format
mla apa chicago
Your Citation
Leahy, Paul. "Types of Exceptions." ThoughtCo, Mar. 3, 2017, thoughtco.com/types-of-exceptions-2033910. Leahy, Paul. (2017, March 3). Types of Exceptions. Retrieved from https://www.thoughtco.com/types-of-exceptions-2033910 Leahy, Paul. "Types of Exceptions." ThoughtCo. https://www.thoughtco.com/types-of-exceptions-2033910 (accessed November 24, 2017).