Thursday, February 26, 2009

source code trycatch



// Good example to use try and cautch blocks
package exceptions;

public class SimpleEx {
public static void main(String ar[])
{
try
{
System.out.println(10/0);
}
catch(ArithmeticException e)
{
System.out.println("caught"); // caught
// for see exception information these three methods


//to print exception stack this method
e.printStackTrace(); //don't put in the inside sop.


//to print exception name and message use this method
System.out.println(e.toString());


// to print only message
System.out.println(e.getMessage());

}
}
}

output:
java.lang.ArithmeticException: / by zerocaught

at exceptions.SimpleEx.main(SimpleEx.java:9)
java.lang.ArithmeticException: / by zero
/ by zero



0 comments:

Post a Comment