Types of Exception in Java



What is an Exception ?

  • In Java, exceptions exist as a class,java.lang.Exception, with two main subclasses, IOException and RuntimeException. Checked exceptions occur when the program is compiled for the most part, the program should be able to recover from these.
  • These can be things like FileNotFoundException, for example, or InvalidInputException. There are three types of exception the checked exception, the error and the runtime exception.

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. 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.
  • 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:
 Exception Image in Java

Learn - tutorial - Exception Image in Java - examples - programs

public FileReader(String fileName)
  throws FileNotFoundException
  • The constructor specifically states that the FileReader constructor can throw a FileNotFoundException.
public static void main(String[] args){
        FileReader fileInput = null;

        //Open the input file
        fileInput = new FileReader("Untitled.txt");
    }
  • 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");
    }
  • 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
        }
    }

Errors :

  • When an exception occurs the JVM will create an exception object. These objects all derive from the Throwableclass. 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.
  • Errors and Runtime Exceptions fall into the category of unchecked exceptions.

Related Searches to Types of exception in java