Exception Hierarchy
- Throwable class access root for java exception hierarchy.
- Throwable class define 2 child class.
- Exception
- Error
Exception:
- Most of the times exception are caused by our program and these are recoverable.
- If our programming requirement is to read data from remote file locating at London, at runtime if remote file is not available, then we will get runtime exception saying FileNotFoundException .
- If FileNotFoundException occurs, we can provide local file and continue rest of the program normally.
Error:
- Most of the times error are not caused by program, these are due to lack of system resources.
- Errors are non recoverable.
- If OutOfMemoryError occurs, being a programmers we can't do anything and program will be terminated abnormally.
- System admin or server admin is responsible to increase heap memory.
Checked Vs Unchecked Exception
Checked Exception:
The exceptions which are checked by compiler for smooth execution of the program are called Checked Exception.
Ex: FileNotFoundException
In our program if there is a chance of raising checked exception then compulsory we should handle this exception. ( either by try-catch or by throws keyword ). Otherwise we will get compile time error.
Unchecked Exception:
The exceptions which are not checked by compiler, whether programmer handling or not.
Ex: ArithmeticException NullPointerException .......
Note:
- Whether it is checked or unchecked, every exception occurs at runtime only. There is no chance of occurring any exception at compile time.
- RuntimeException and its child classes, Error and its child classes are unchecked, except these remaining are checked.
Fully checked Vs Partially checked
- A checked exception said to be fully checked if and only if all it's child classes are checked.
Ex: IOException InterruptedException
- A checked exception said to be partially checked if and only if some of it's child classes are unchecked.
Ex: Throwable Exception
Note: The only possible partial checked exception in java are:-
- Exception
- Throwable