Hi,
I have the following code block -
public class ExceptionTest {
public static void main(
String[] args) {
try {
ExceptionTest
test = new ExceptionTest();
test.foo();
} catch (MyException ex) {
System.out.println("An Error!!");
//ex.printStackTrace();
}
}
public void foo()
throws MyException {
int j = 0;
try {
j = 5 / 0; // will give an error since you cannot divide by zero
} catch (Exception e) {
throw new MyException();
} finally {
System.out.println("Shucks");
}
}
}
class MyException
extends Exception {
public MyException() {
System.out.println("MyException() Constructor Invoked");
}
}
I am getting an output of
MyException() Constructor Invoked
Shucks
An Error!!
According to the exception handlign mechanish, if there is a finally block, then, execution continues after the exception is caught into the finally block. Then, how is it showing the message in the exception block of the calling method? Also, is it safe to have clean-up code in the finally block of individual methods?
TIA,
Manoj.