Java Exceptions

Sometimes the program may fail at a particular line due to invalid input, or invoking another application’s method which is down will lead to blow up the entire program. If we know these statements are risky in nature then, we need to handle it. This process is said to Exception Handling.

Exception Handling:

By default, Java has its own exception handling provided by the run time. An Exception is nothing but an object of type Exception.

When an error is occurred, the default exception handler will create an object of type Exception and gives it to the run time system. This makes the program to stop.  This process is called as Throwing an exception.

Catching Exceptions:

Rather than leaving the job everytime to the default handler, we can also catch and handle exceptions on our own. Because there are different types of defined exceptions that can occur and each requires a different remedy when it occurred. We can even have our own exception type by extending the Exception class. We can see this in the later Inheritance chapter on how to extend a class.

So if we know that a piece of code is risky then we can put them inside the try statement.

Try, Catch & Finally Block:

If the code inside the try block blows, then the control goes to the correct catch block based on the exception type and then to the finally block.

If the code inside the try block works fine, then the catch block is skipped and the control goes to the finally block.

Even if the try or catch statement has a return statement, finally will always gets executed.

  // Your main code here	
  } catch(ExceptionType1 exp){
 	// Code needs to be executed after the ExceptionType1 occurred and caught.			
  } catch(ExceptionType2 exp){
	// Code needs to be executed after the ExceptionType2 exception occurred and caught.			
  } finally {		
	// Final piece of code after the exception is handled.

If you define a custom Exception type then, it needs to be thrown by your code.

Duck an exception:

Assume a place where you are throwing an exception and you don’t want to handle it. In this case, you can simply duck it to your parent methods as below. This can be done simply by adding “throws ExceptionType” statement to your method. They will have to handle this by using try catch blocks in this case.

public void execute(int a) throws Exception{	
	if(a <= 0){
          throw new Exception("a is less than or equal to zero, "
		+ "Please send a value greater than zero to process.");
	// Business logics here.

Sample Program:

public class ExceptionClass {	
	public static void main(String[] args) {
	  // Setting null on purpose to get the exception.
	  String words = null;		
	      System.out.println("It contains the letter s");				
	  } catch(NullPointerException exp){
	    System.out.println("Caught the Null pointer exception.");			
	  } catch(ArrayIndexOutOfBoundsException exp){
            System.out.println("Caught the ArrayIndexOutOfBoundsException exception.");
	  } finally {		
	    System.out.println("Shutdown the system.");


Caught the Null pointer exception.
Shutdown the system.

Posted on July 1, 2014 in Java for Beginners

Share the Story

Leave a reply

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

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

Back to Top