Exceptions in Java

What is an Exception

Exceptions are similar to errors, but they occur at runtime. These are unexpected and/or unwanted events that hinder the natural flow of a program and result in the program crashing.

Error vs Exception

Error: Errors are normally obvious problems that are pretty evident on seeing the source code implementing the program. A reasonable application should not normally catch such problems.

Exceptions: Exceptions are events that are not generally evident from the source code itself and result in the code crashing. These are conditions that any application in any domain must try and catch to enable risk minimalization.

Exception Hierarchy

All errors and exceptions are subclasses of the throwable class in Java. This acts as the superclass of the hierarchy throwable branches into. One is the exceptions branch, while the other is the error branch. The exceptions class is used when the user wants to catch unexpected errors in a code. One example is the NullPointerException. On the other hand, errors are used by the JVM (Java Runtime Environment) to point out errors in the Java Runtime Environment (JRE). One such example is a logical error like StackOverflowError.

How does the JVM handle an Exception?

Default Exception Handling: Whenever an exception is caught inside a method, an Exception Object s created. This object is then passed on to the JVM. This object has all the necessary details of the exception, including the name, description of the exception, and the present state of the source code. Handling an exception is the complete process of sending an exception from its nascent stage finally to the runtime environment. A situation as follows may arise where a series has been called to reach the method that throws an exception. This ordered list of the methods is called Call Stack. Now the next steps to this situation are as follows:

  • The JRE will go through the stack of method calls to reach the method that generates an exception. This block of code is called the Exception handler.
  • The JRE system begins its search from the method that generated the exception but traverses through the call stack in reserve order now.
  • The JRE searches for a suitable exception handler for the exception. If it does, the exception is handled successfully. A suitable exception here means that the type of the exception object thrown matches the type of the exception object it can handle.
  • Suppose the runtime system traverses through the entire call stack but is unable to find an appropriate exception handler for the exception. In that case, it is passed on to the default exception handler. The default handler prints out an exception message in the following format, and the program terminates immediately:
  • Exception in thread “xxx” Name of Exception : Description …….. //Call Stack

Sample Code 1

Consider the simple program here:

class ThrowsExcep
{
public static void main (String args [])
{
String str = null;
System.out.println (str.length());
}
}

 

OUTPUT

Exception in thread “main” java.lang.NullPointerException at ThrowsExecp.main (File.java:8)

Sample Code 2

The following code demonstrates how runtime searches for an exception handler from the call stack.

class ExceptionThrown
{
static int divideByZero (int a, int b)
{
int i = a/b;
return i;
}
static int computeDivision (int a, int b)
{
int res = 0;
try
{
res = divideByZero (a, b);
}
catch (NumberFormatException ex)
{
System.out.println (“NumberFormatException is occured”);
}
return res;
}
public static void main (String args [])
{
int a = 1;
int b = 0;
try
{
int I = computeDivision (a, b);
}
catch (ArithmeticException ex)
{
System.out.println (ex.getMessage());
}
}
}

 

OUTPUT

/ by Zero

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.