Exception Handling

Exception Handling

In this tutorial, we are going to discuss Exception handling in java. Exception handling is a powerful mechanism to prevent the exception during the execution of the program. It enables a program to complete the execution even if an exception occurs in the program.

  • An exception is an unwanted event that occurs during the execution of the program.
  • It is highly recommended to handle exceptions.
  • Exception handling doesn’t mean repairing an exception. It means providing alternative way to continue rest of the program normally.
  • An exception handling in java is different from the error. Error is a severe problem that cannot be tried to catch during the program execution, while exception can be handled using the statement’s try-catch block.

For example, if our programming requirement is to read data from a file which is available at the remote machine and at run time, if that remote machine file is not available, then our program should not be terminated abnormally.

As an alternative, provide a local file to continue the rest of the program normally. This way of providing alternative ways is nothing but Exception Handling.

The main objective of Exception Handling is graceful termination of the program (Normal termination of the program).

Runtime Stack Mechanism
  • For every thread, JVM will create a runtime stack.
  • All the method calls performed by the thread will be sorted in the corresponding runtime stack.
  • Each entry in the Runtime stack is called stack frame or activation record.
  • Once if the method execution completes corresponding entry will be removed from the Runtime Stack.
  • Once all the methods execution is completed, then an empty runtime stack will be destroyed by JVM just before terminating the thread.
  • This entire process is called the Runtime Stack mechanism.
public class ExceptionDemo {
   public static void main(String[] args) {
      doStuff();
   }

   public static void doStuff() {
      doMoreStuff();
   }

   public static void doMoreStuff() {
      System.out.println("Hi this is Exception ...........Thread");
   }
}
Exception Handling
Default Exception Handling
public class ExceptionDemo {
   public static void main(String[] args) {
      doStuff();
   }
   public static void doStuff() {
      doMoreStuff();
   }
   public static void doMoreStuff() {
      System.out.println(10/0);
   }
}

Exception in thread "main" java.lang.ArithmeticException: / by zero

In our program, while executing any method, if an exception is raised, the corresponding method is responsible for creating an exception object by including the following information.

  1. Name of Exception.
  2. Description.
  3. Location of Exception.

After preparing the Exception Object, The method handovers the object to the JVM. JVM will check for Exception handling code in that method. If the method doesn’t contain any exception handling code, JVM terminates that method abnormally and removes the corresponding entry from the stack.

JVM will check for exception handling code in the caller. If the caller method doesn’t contain exception handling code, JVM terminates that caller method abnormally and removes the corresponding entry from the stack.

This process will be continued until the main method, and if the main method also doesn’t contain any exception handling code, then JVM terminates the main method abnormally.

Just before terminating the program, JVM handovers the responsibilities of exception handling to the default exception handler. Default exception handler prints the error in the following format.

Name of Exception : Description stackTrace
Exception Hierarchy

Throwable is the parent of entire java exception hierarchy. It has 2 child classes

1. Exception

These are recoverable. Most of the cases exceptions are raised due to program code only.

2. Error

Errors are non-recoverable. Most of the cases errors are due to a lack of system resources but not our programs.

Checked Vs UnChecked

The Exceptions which are checked by the compiler for smooth execution of the program at runtime are called ‘checked exception.’

E.g

IOException, ServletException, InterruptedException.

The Exceptions which are unable to be checked by the compiler are called ‘unchecked exceptions.’ RuntimeException and its child classes, Error, and its child classes are considered unchecked exceptions, and all the remaining are checked.

Whether the exception is checked or unchecked, it always occurs at runtime only.

Partially checked Vs fully checked

A checked exception is said to be fully checked if and only if all its child classes are also checked.

E.g

IOException

A checked exception is said to be partially checked if some of its child classes are not checked.

E.g

Exception, Throwable
Exception Handling
Scroll to top