Home >> Java >> Detailed Concept of Java Exceptions

Detailed Concept of Java Exceptions

Definition of Exception

An unwanted and unexpected event during program execution which disrupt the normal flow of execution of program is called Exception.

Exception object

The exception may occur by various reasons. In java for each exception, program generate an object of the Exception type and handles it to the run-time system. The object generated due to exception is called Exception object. Exception object contain information about type, location and description of the exception. It means there are many exception classes, which type of object is created in case of exception.

A figure to describe the Exception:

Java Exceptions


Let’s describe with an example. If we divide any number by zero, the system will generate exception object of type ArithmeticException. Also if we try to access the content of null object, then NullPointerException object is created. Where ArithmeticException is one of the subclass of class Exception.  Examples of frequent exceptions are FileNotFoundException, IOException, NullPointerException etc.


The process of passing thus generated Exception object to the run time system is called throwing. Exception contains information about type and state of exception. Then run-time system seek way to handle the Exception. It searches all the methods on stack that may contain Exception handling code. The class Exception extends from class Throwable.

What happens when Exception occurs?

Answer is, The JVM search for Exception handling code in the method where exception occurred. If it does not found the exception handling code, then JVM terminates that method abnormally and method entry is removed from run-time stack.

Then JVM identifies caller method and checks whether caller method contain any handling code or not. If the caller method does not contain exception handling code JVM will do same for this method as previous one i.e. method is terminated abnormally and removed from stack. This process will be continued until it reaches to main method.

If JVM still not found any exception handling code in main method then JVM terminates main method also abnormally and empty the stack.

Default Exception handler

Then JVM handovers responsibility of exception handling to a special part of JVM  called Default Exception Handler. Default exception handler prints exception information in the following format and terminates program abnormally.

Format of Exception Information:

Exception in thread “nameOfThread” NameOfException: Exception message

Description of stack trace

Example of Exception information format



The simple paint figure to describe the stack for this(TestException.java) program is here. Take a glance here..

Java Exceptions - ExceptionStack Small Description

In above program exception occur in the method methAgain() because we attempted to print the result of dividing a number by zero. When Exception is occurred the run-time system seek for exception handling code to the calling methods. In this case there is no handling code so JVM will handle the Exception object to the default exception handler and then program terminated after printing exception information.

Output of above program:

OutputException - Java Exceptions

At Last:

I wish you must get following thing at least after reading this post.

  • Exception can terminate  program abnormally if not handled.
  • Rest of the code after Exception is not executed.
  • JVM will search for handling code in the calling methods

About Log Raj Bhatt

Leave a Reply

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