try {
// code that might contain an exception
} catch (ExceptionType e) {
// code to deal with the exception
}
public class ExceptionExample {
public static void main(String[] args) {
int i;
i = -1;
double f;
while (i < 2) {
System.out.println();
try {
System.out.printf("Computing 1/%d\n",i);
f = 1/i;
System.out.printf("1/%d = %f\n",i, f);
} catch (Exception e) {
System.out.printf("Exception Caught: %s\n",e.getMessage());
System.out.printf("That is also: %s\n", e.toString());
}
++i;
}
}
}