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:
- Java – Exception Handling
- Java – Exception Hierarchy
- Java – 5 important keywords in Java Exception handling
- Java – Runtime mechanism, what happens when exception is thrown ?
- Java – Checked Exception v/s Unchecked Exception
- Java – Exception propagation
- Java – try-catch block
- Java – finally block
- Java – try with multiple catch blocks
- Java – Nested try-catch block
- Java – Returning value from method having try-catch-finally blocks
- Java – return statement with finally block
- Java – final v/s finally v/s finalize
- Java – Various methods to print exception information
- Java – throw keyword
- Java – throws keyword
- Java – throw v/s throws
- Java – Difference between throws clause and try-catch-finally block
- Java – Rules for Exception handling w.r.t Method Overriding
- Java – User-defined or Custom exception
- Java – Difference between ClassNotFoundException v/s NoClassDefFoundError
- Java – Top Exception and Error
- Java – Interview question and answers on Exception Handling
- Java 7 – try with resources
- Java 7 – multi-catch block
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 !!