In this post, you will learn to:
- Explain the concept of Exceptions.
- Identify the different types of Exceptions
Causes for Exceptions
An exception is an abnormal condition that arises out of an extraordinary situation disrupting the flow of program’s instructions. Exceptions report error conditions in a program. In programs, exceptions can occur due to any of the following reasons:
These exceptions arise due to errors present in APIs, such as NullPointerException. The program using these APIs cannot do anything about these errors.
Client code errors
The client code attempts operations, such as reading content from a file without opening it. This will raise an exception. The exception will provide information about the cause of the error and is handled by the client code.
Errors beyond the control of a program
There are certain exceptions that are not expected to be caught by the client code, such as memory error or network connection failure error. These are errors which have been raised by the run-time environment.
Classification of Exceptions
In Java there are two types of exceptions and they are as follows:
Checked exceptions are generated in situations that occur during the normal execution of a program. Some common examples of checked exceptions are: requesting for missing files, invalid user input, and network failures. These exceptions should be handled to avoid compile-time errors. If an exception occurs during the execution of a method, the method can handle the exception itself or throw the exception back to the calling method to denote that a problem has occurred. The calling method can again handle the exception itself or throw it to its calling method. This process can continue until the exception reaches the top of a thread and the thread is terminated. This is known as the Call-Stack mechanism. The main advantage of this practice is that a developer has the flexibility of putting the error handling code wherever he chooses.
These types of exceptions are derived from the Exception class. The program either handles the checked exception or moves the exception further up the stack. It is checked during compilation.
Unchecked exceptions are generated in situations that are considered non-recoverable for a program. Common examples of these situations are attempting to access an element beyond the maximum length of an array. An application is not required to check for these kinds of exceptions. Runtime exceptions are also examples of unchecked exceptions. They generally arise from logical bugs. Unchecked exceptions that arise on account of environmental problems or errors are impossible to recover from and are known as Errors. Exhausting a program’s allocated memory is a common example of an error.
These exceptions are derived from the RuntimeException class, which in turn is derived from the Exception class. The program need not handle unchecked exception. It is generated during the execution of the program.
The figure below shows the exception hierarchy in Java.
When an error occurs in a method during runtime, an object is created containing information about the error, its type, and the state of the program. This object is referred to as exception object. Exception objects are instances of classes that are derived from the base class Throwable.
Types of Checked Exceptions
All Checked exceptions are derived from the Exception class. The table below lists some of the common Checked exceptions.
|InstantiationException||This exception occurs if an attempt is made to create an instance of the abstract class.|
|InterruptedException||This exception occurs if a thread is interrupted.|
|NoSuchMethodException||This exception occurs if the Java Virtual Machine is unable to resolve which method is to be called.|
|RuntimeException||This exception may be thrown during normal operation of Java Virtual Machine if some erroneous condition occurs.|
Types of Unchecked Exceptions
All Unchecked exceptions are directly or indirectly derived from the RuntimeException class.
The table below lists some of the common Unchecked exceptions.
|ArithmeticException||Derived from RuntimeException and indicates an Arithmetic error condition.|
|ArrayIndexOutOfBoundsException||Derived from IndexOutOfBoundsException class and is generated when an array index is less than zero or greater than the actual size of the array.|
|IllegalArgumentException||Derived from RuntimeException. Method receives an illegal argument.|
|NegativeArraySizeException||Derived from RuntimeException. Array size is less than zero.|
|NullPointerException||Derived from RuntimeException. Attempt to access a null object member.|
|NumberFormatException||Derived from IllegalArgumentException. Unable to convert the string to a number.|
|StringIndexOutOfBoundsException||Derived from IndexOutOfBoundsException. Index is negative or greater than the size of the string.|
Exception Handling in Java
User-defined Exceptions in Java
Assertions in Java