Introduction

Exception Handling
  • An unwanted unexpected event that disturbs normal flow of the program is called Exception.
  • 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.

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

As  alternative provide a local file to continue rest of the program normally. This way of providing alternative way 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 if all the methods execution completed then empty runtime stack will be destroyed by JVM just before terminating the thread.
  • This entire process is called 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");
   }
}
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 raised then the corresponding method is responsible to create exception object by including the following information.

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

After preparation of 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 then JVM terminates that method abnormally and removes corresponding entry from the stack.

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

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

Just before terminating the program JVM handovers the responsibilities of exception handling to 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 lack of system resources but not due to 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 checked by the compiler are called β€˜unchecked exceptions’. Runtimeexception and it’s child classes, Error and it’s child classes are considered as unchecked exceptions and all the remaining considered as checked.

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

Partially checked Vs fully checked

A checked exception is said to be fully checked iff all it’s child classes also checked.

E.g

IOException

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

E.g

Exception, Throwable
Introduction
Scroll to top