In this article we will discuss what is an exception in Java and how to handle exception in Java.

What is an exception in Java

An error occurred at runtime in a Java Application is called an Exception. If a triggered Exception is not handled then it will cause the application to crash.

Checkout following code,

public class Example1 {

	public static void main(String[] args) 
	{
		String strObj = null;
		
		int len = strObj.length();
		
		System.out.println("len = " + len);

	}

}

Above example will crash abruptly with following output,
Exception in thread "main" java.lang.NullPointerException
	at Example1.main(Example1.java:8)

That’s because String reference strObj was set to null and a member function was called with it. Hence a runtime error occurred and an exception was thrown i.e. NullPointerException. But this code didn’t handle the triggered exception and application crashed.

How to handle an exception in Java

When an exception will occur then an Exception class Object will be created and Java Runtime will search for its handler.

 

Code for which we want to handle the triggered exception must be in try block i.e.

try

{

………..

………..

}

Now exception triggered inside the try block should be handled by a catch block after it i.e.
catch(Exception e)

{

...........

.........

}

When an exception occurs then Runtime will search of its Exception Handler i.e a catch block. First of all it will search in the current function where exception has occurred.

Exception Handling in Current Function:

Let’s see, how we can catch the exception in previous example by enclosing the code in try block and handling the exception in catch block i.e.

public class Example2 {

	public static void main(String[] args) 
	{
		int len = 0;
		try
		{
			String strObj = null;
			len = strObj.length();
		}
		catch(NullPointerException e)
		{
			System.out.println("It is a Null Pointer Exception");
		}
		
		System.out.println("len = " + len);

	}

}

Output:
It is a Null Pointer Exception
len = 0

Here, NullPointerException was catched in the same function and handled accordingly. From the line where exception is triggered execution flow of program will move to catch block directly. It means lines after that will not be executed, as shown in above example.

Handling Exception in Parent Functions

Here code that triggered the exception was in try block and catch block was in the same function. But what if there is appropriate handler in the function where exception has occurred?

If Java Runtime does not find any handler in function where exception has triggered, then it will search in its parent function from the call stack and search will go on till it finds any handler or main function is reached. If it reaches the main() function and still find no handler then it will make the application to crash.

Let’s see how to handle exception in parent function,

public class Example3 {

	public static void fun()
	{
		String strObj = null;
		int len = strObj.length();
		System.out.println("len = " + len);
	}
	public static void main(String[] args) 
	{
		try
		{
			Example3.fun();
		}
		catch(NullPointerException e)
		{
			System.out.println("It is a Null Pointer Exception");
		}
		
		System.out.println("Main Exits");
	}
}

Output
It is a Null Pointer Exception
Main Exits

 

Here exception was handled in the parent function.

Join a list of 2000+ Programmers for latest Tips & Tutorials