Causes of 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:
- Programming errors: 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
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.
In Java there are two types of exceptions. They are:
- Checked Exceptions: 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 Exception: 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.
Types of Checked Exceptions
All checked exceptions are derived from the Exception class. Some of the common checked exceptions are:-
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 directly or indirectly derived from the RuntimeException class. Some of the common Unchecked exceptions are described:
ArithmeticException: Derived from RuntimeException and indicates an Arithmetic error condition.
ArrayIndexOutOfBoundsException: Derived from RuntimeException. Method receives an illegal arugument.
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.