Exception Hierarchy in Java

Java provides a comprehensive hierarchy of exceptions to categorize and handle different types of exceptional events. In Java, the exception hierarchy is represnt by a class hierarchy where the root class is Throwable. Throwable class defines two child classes –

  • Exception
  • Error

Exception

Most of the time exceptions are caused by our own program and it is recoverable.

For Example- If our programing requirement is to read data from another location at run time but it is not available then we will get a runtime exception saying- “FileNotFoundException”, If this exception occurs then we can provide alternative logic and the program continue normally.

try{
   // read data from another location
}
catch( file not found exception){
// Provide alternative logic
}

Error

Most of the time errors are not caused by our Java program, Error occurs due to a lack of system resources. Errors are not recoverable.

For Example- If of memory error occurred then as a programmer we can not do anything and the program will terminate abnormally.

Similar Java Tutorials

4 thoughts on “Exception Hierarchy in Java”

Leave a Comment