What is Exception and Exception hierarchy in Java?

What is Exception and Exception hierarchy in Java?

  • Throwable class act as the root for Java Exception Hierarchy.
  • A throwable class defines two child classes
    1. Exception
    2. Error

Exception

  • An unexpected unwanted even that disturbs normal flow of the program is called an exception.
  • It is highly recommended to handle the exceptions and the main objective of exception handling is graceful termination of the program. 
  • Exception Handling doesn’t mean repairing an exception. We have to provide the alternative way to continue rest of the program normally is the concept of Exception handling. 
  • Most of the times exceptions are caused by our program and these are recoverable. For example, if our programming recruitment is to read data from remote file locating at Masco 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 errors are not caused by our program and these are due to lack of system resources.
  • An error is non-recoverable.
  • For example, OutOfMemoryError occurs being a programmer we can’t do anything and the program will be terminated abnormally.
  • System admin or server admin is responsible to increase heap memory.

Exception Hierarchy Diagram

What is Exception and Exception hierarchy in Java?

What is Exception and Exception hierarchy in Java?

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*