Previous | Home | Next |
If method not capable of causing exception that it does not handle, it must specify this behavior so that callers of the method guards themselves against it. Throws clause lists the type of exception that method throws. The general format of the clause is :
type method_name(parameterlist)throws ExceptionList { // body }
Example
/* * Save as implementthrows.java. * Using Throws */ package UsingThrows; class Demo { void throwExcp()throws IllegalAccessException { System.out.println("Inside throwExcp"); throw new IllegalAccessException(); } } public class implementthrows { public static void main(String[] str) { try { Demo obj=new Demo(); obj.throwExcp(); } catch(IllegalAccessException e) { System.out.println("Caught "+ e); } } }
Output:
Inside throwExcp Caught java.lang.IllegalAccessException
Previous | Home | Next |