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
- Odd Even in Java
- Fibonacci Series Java
- Switch statements in Java
- For loop in Java
- While loop in Java
- Do while loop in Java
- Import statement in Java
- Abstract modifier in Java
- Strictfp modifier in Java
- Final variable in Java
- Adapter Class in Java
- Method signature in Java
- Method overloading in Java
- Has A Relationship
- Inheritance in Java
- Encapsulation in Java
- Abstraction in Java
- Data Hiding in Java
- Interface vs abstract class
- Method Overriding in Java
- Method Overloading in Java
- Coupling in Java
4 thoughts on “Exception Hierarchy in Java”