JAVA: Exceptions

One of the features of the Java programming language is its robustness. This means that errors are minimized and that errors should be effectively handled. Although the ideal time to trap an error is during compile time, all errors can’t be detected. Errors that occur at runtime are called Exceptions. Tracking these exceptions during run-time is termed asException Handling. Java’s exception handling feature takes care of handling and recovery of the exceptions occurred.

What is an Exception?

When an error is encountered during the execution of a program, an exception occurs. In other words, exception is a runtime error. This may happen because of the occurrence of certain events that arise at the time of execution. For example, division by zero in a program will cause an error.

When an exception occurs, the program terminates abruptly and control returns to the operation system (OS). Languages, which do not support execution handling, have to take care of errors manually. It is important to ensure that the program does not terminate abruptly due to the occurrence of an exception. Exception handling is performed to identify errors and trap them.

Handling Exceptions

Whenever an exception occurs in a program where proper execution handling mechanism is not provided, the program aborts leaving all resources allocated in the same state. This can lead to a resource leak. For example, memory allocated for the program cannot be de-allocated for other programs to use. To prevent such instances from occurring, an effective Exception Handling mechanism is required where all resources allocated to the file such as memory may not return to the OS.

Error handling in Java is performed with the help of an exception-handling model. According to this model, when an error occurs, an exception is thrown. This exception is caught in a block of code. Exceptional conditions are treated as distinct from the normal flow of control in the program. Exceptions have to be caught otherwise the program exits. Exceptions can be generated by the Java run-time system, or they can be manually generated by the code.

In Java, when an exception occurs, an instance representing that exception is created. This instance contains detailed information about the exception. The instance is then passed to the method that retrieves and processes the information.

Exception Handling in Java is managed through five keywords:

  • try
  • catch
  • throw
  • throws
  • finally

Program statements, that have to be mentioned for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. By using catch keyword, the programmer can catch the exception and handle it in some rational manner. System generated exceptions are thrown automatically by the java run-time system.

To manually throw an exception, the keyword throw is used. The throws clause is used in a method to specify that this method will throw out an exception. In the finally block, the code that is absolutely necessary to be executed before the program terminates, is specified. In java, creating an exception object and handling it over to the runtime system is known as throwing an exception.

The Error and Exception classes are used for handling errors in Java. These classes are derived from Throwable class which in turn is derived from Object class. Only objects that are instances of the Throwable, Error, Exception class or any other class derived from them are recognized as exceptions by the JVM. If the user wants to create a custom defined exception then the class should be a subclass of the Exception class. There is an important subclass of Exception class called RuntimeException. This subclass includes all the common runtime exceptions like division by zero, invalid array indexing and so on.

The following code illustrates usage of the ­try catch block:

class ExceptionDemo
{
    public static void main(String args[])
    {
        try
        {
            String text = args[0];
            System.out.println(text);
        }
        catch(Exception ne)
        {
            System.out.println("No arguments given! ");
        }
    }
}

The output of the program is: