The built-in exceptions in Java are categorized on the basis of whether the exception is handled by the Java compiler or not. Java consists of the following categories of built-in exceptions:
- Checked Exceptions
- Unchecked Exceptions
Checked Exceptions
Checked exceptions are the objects of the Exception class or any of its subclasses excluding the Runtime Exception class. Checked exceptions are the invalid conditions that occur in a Java program due to invalid user input, network connectivity problem, or database problems. For example, java.io.IOException is a checked exception. The IOException exception is thrown whenever an input/output operation is abnormally terminated.
Java uses the try-catch block to handle the checked exceptions. The statements within a program that throw an exception are placed in the try block. You associate an exception-handler with the try block by providing one or more catch handlers immediately after the try block.
The following table lists the various checked exceptions defined in the java.lang package:
|
Exception |
Cause of Creation |
| ClassNotFoundException |
Occurs when the Java run-time |
| IllegalAccessException |
Occurs when you want to refer a |
| InstantiationException |
Occurs when you try to create an |
| NoSuchMethodException |
Occurs when you call a method that |
Unchecked Exceptions
Unchecked exceptions are the run-time errors that occur because of programming errors, such as invalid arguments passed to a public method. The Java compiler does not check the unchecked exceptions during program compilation. For example, if you divide a number by zero, an unchecked or run-time exception is raised.
The following table lists the various unchecked exceptions:
|
Exception |
Cause of Creation |
| ArithmeticException |
Occurs when you make an |
| ArrayIndexOutOfBoundsException |
Occurs when an attempt is made |
| ArrayStoreException |
Occurs when you assign an |
| ClassCastException |
Occurs when you assign a |
| IllegalArgumentException |
Occurs when you pass an |
| NegativeArraySizeException |
Occurs when you create an array |
| NullPointerException |
Occurs when an application tries to |
| NumberFormatException |
Occurs when you want to convert |