Exception Handling in Java

Exception : Exception is an abnormal condition that arises during the execution of the Program. In other words, an exception is a run-time error.
Exception Handling : Exception Handling is a powerful mechanism to handle run time errors (exceptions) in the program so that normal flow of the program can be maintained. This makes our Java program robust .
Important points :

Dictionary meaning of the exception is abnormal termination.
An exception is a problem occurred during execution time of the program
An unwanted unexpected event that disturbs normal flow of execution called exception .
It is an object which is thrown at runtime.
A Java exception is an object that describe a condition of run time error that has occurred in our java program during the execution of our program. When an exception arises in program , an object representing that exception is created and thrown in the method that causes the error.

Advantage of Exception Handling

  The main advantages of Exception Handling is to maintain the normal flow of the application/program .

  Exception Handling mechanism makes java application robust.

Types of Exception

As per sun micro systems standards The Exceptions are divided into three types
1) Checked Exception
2) Unchecked Exception
3) Error

1) Checked Exception

The Exceptions which are checked by the compiler at compilation time for the proper execution of the program is called Checked Exception.

  The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.

2) Unchecked Exception

The exceptions which are not checked by the compiler at compilation time is called Unchecked Exception .
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
Some of the unchecked exceptions in the java language:-

Exception Exception Description
ArithmeticException Arithmetic error, such as divide-by-zero.
ArrayIndexOutOfBoundsException Array index is out-of-bounds.(out of range)
InputMismatchException If we are giving a input which is not matched for storing input.
ClassCastException If the conversion is Invalid (Invalid Type Casting).
IllegalArgumentException Illegal argument used to invoke a method.
IllegalThreadStateException Requested operation not compatible with current thread state.
IndexOutOfBoundsException Some type of index is out-of-bounds.
StringIndexOutOfBoundsException Attempt to access outside the bounds of a string.
NumberFormatException Invalid conversion of a string to a numeric format.
NullPointerException Invalid use of a null reference. Trying to call a method on object which contain null reference.
NegativeArraySizeException Array created with a negative size.

3) Error

Errors are caused by lack of system resources . these are non recoverable. for example :- StackOverFlowError , OutOfMemoryError ,AssertionError etc.

Exception Hierarchy

  Subclasses of RuntimeException and Error are the Unchecked.

  Subclasses of Exception other than subclasses of RuntimeException are the Checked Exceptions.

Difference between checked Exception and un-checked Exception

Checked Exception Un-Checked Exception
checked Exception are checked at compile time un-checked Exception are checked at run time
e.g. FileNotFoundException, NumberNotFoundException etc. e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

Difference between Error and Exception

Error Exception
These are the errors which can not be handled by programmer These are the run time errors which can be handled by programmer.
Example: NoSuchMethodError OutOfMemoryError Example: ClassNotFoundException NumberFormateException