In this article, we will discuss exception hierarchy in detail with figure and explanation
Exception Hierarchy:
Throwable class is the root class for every exception and it branches out to 2 main categories i.e.;
- Exception
- Error
1. java.lang.Throwable:
- Throwable is the root class for exception & it’s sub-type and error & it’s sub-types
- In other words, it is super class for exception & error
- java.lang.Throwable class extends java.lang.Object class (as shown in the above figure)
- It defines 2 sub classes i.e.; Exception and Error
2. java.lang.Exception:
- java.lang.Exception is super class for all types of Exception
- It extends java.lang.Throwable class
- Exception are due to programmatic logic
- And it is recoverable
- Exception are categorized into checked exception and unchecked exception
- Example: RuntimeException, SQLException, IOException, FileNotFoundException, ArithmeticException, NullPointerException
3. java.lang.Error:
- java.lang.Error is super class for all types of Error
- It extends java.lang.Throwable class
- Error are due to lack of system resources
- And it is non-recoverable
- All error fall into unchecked exception category, as it is raised due to lack of system resources at runtime
- It is out of programming scope as such type of error can’t predicted, may be well planned care can be taken to avoid these kind of Error
- Example: VirtualMachineError, AssertionError, ExceptionInInitializerError, StackOverflowError, OutOfMemoryError, LinkageError, InstantiationError
Note: above mentioned Exception and Error are again categorized into checked and unchecked exceptions
Related Articles:
- Exception Handling in Java
- Runtime mechanism in Java – what happens when exception is thrown?
- Checked Exception v/s Unchecked Exception
- try-catch block in Java
- finally block in Java
- Checked Exception v/s Unchecked Exception
References:
- https://docs.oracle.com/javase/tutorial/essential/exceptions/
- https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html
- http://www.oracle.com/technetwork/java/effective-exceptions-092345.html
- http://otfried.org/courses/cs206/slides/slides-stackframes.pdf
Happy Coding !!
Happy Learning !!