Friday, February 27, 2009

Example code using throws



package exceptions;
// throws keyword for delegating the responsibility of the Exception handling to the caller
public class ThrowsEx {
public static void main(String a[])throws Exception //without throws error
{
doStuff();
}
public static void doStuff()throws Exception //without throws error
{
doMoreStuff();
}
public static void doMoreStuff()throws Exception //without throws error
{
System.out.println("hai");
Thread.sleep(100); // compile time error
//if we are using above code which is possible to rise the checked exception.
//we must have to use eaither throws,try,catch.
}
}



0 comments:

Post a Comment