Exceptions in Java

Exceptions in Java


Posted in : Core Java Posted on : October 1, 2010 at 4:20 PM Comments : [ 0 ]

This section contains the detail about the Exceptions in Java .

Exceptions in Java

In java , exception is an object that describe an erroneous condition that has occurred in as piece of code. Java exception handling is managed through five keywords : try, catch , throw, throws and finally.

In try block, we put the code which you want to monitor for exception. You can handle the exception thrown by the code using catch block. To throw exception manually, use keyword throw. Any exception that is thrown out of a method must be specified as such by throws clause. The code which must be executed, before a method returns value, should be placed in finally block .

try and catch example :


public class TryCatch {
public static void main(String args[]) {
int d, a;
try {
d = 0;
a = 42 / d;
System.out.println("This will not be printed" + a);
} catch (ArithmeticException e) {
System.out.println("Divided by zero");
}
System.out.println("After catch statement");
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac TryCatch .java

C:\Program Files\Java\jdk1.6.0_18\bin>java TryCatch
Divided by zero
After catch statement

Throw :

You can throw exception explicitly as follows :

throw ThrowableInstance ;

ThrowableInstance must be an object of type 'Throwable' or a subclass of 'Throwable'.

Example :


public class ThrowDemo {

	static void DevProc() {
		try {
			throw new NullPointerException("demo");
		} catch (NullPointerException e) {
			System.out.println("Caught inside Devproc.");
			throw e;
		}
	}

	public static void main(String args[]) {
		try {
			DevProc();
		} catch (NullPointerException e) {
			System.out.println("Recaught :" + e);
		}

	}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac ThrowDemo .java

C:\Program Files\Java\jdk1.6.0_18\bin>java ThrowDemo
Caught inside Devproc.
Recaught :java.lang.NullPointerException: demo

throws :

If a method is capable of causing exception but it is not using 'catch' to handle this. The 'throws' specify this behaviour so that callers of the method can guard themselves from that exception.

Example :


public class ThrowsDemo {
	static void throwone() throws IllegalAccessException {
		System.out.println("Inside throwone");
		throw new IllegalAccessException("demo");
	}

	public static void main(String args[]) {
		try {
			throwone();
		} catch (IllegalAccessException e) {
			System.out.println("Caught " + e);
		}
	}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac ThrowDemo .java

C:\Program Files\Java\jdk1.6.0_18\bin>java ThrowDemo
Caught inside Devproc.
Recaught :java.lang.NullPointerException: demo

finally

finally creates a block of code that will be executed after a try/catch block, and it will execute whether or not an exception is thrown. This can be useful for closing file handles and freeing up any other resources that might have been allocated.

Note :- The finally clause is optional . However each try block need at least one catch or a finally clause.

Example :

public class FinallyDemo{

   public static void main(String args[]){
      int x[] = new int[2];
      try{
         System.out.println("Element three :" + x[3]);
      }catch(ArrayIndexOutOfBoundsException e){
         System.out.println("Exception thrown  :" + e);
      }
      finally{
         x[0] = 6;
         System.out.println("First element: " +x[0]);
         System.out.println("The finally statement is executed");
      }
   }
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac FinallyDemo.java

C:\Program Files\Java\jdk1.6.0_18\bin>java FinallyDemo
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics