G
getsanjay.sharma
Hello to all programmers out there. I have done a bit of fiddling with
exceptions I have come to some conclusions, which I would like to get
evaluated.
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println(1/0);
}
catch(Exception e)
{
try
{
System.out.println("In catch");
throw new NullPointerException();
}
catch(Exception ee)
{
System.out.println("I have really caught the
exception");
}
}
finally
{
System.out.println("This is finally");
}
System.out.println("After finally");
}
}
``Now my understanding is this. The first line causes an Arithmetic
Exception(divide by zero). This exception is pushed on to the current
stack frame of the executing method. The C runtime of the JVM now
searches for the corresponding catch block and finds one. Hence the
exception just pushed is popped off the frame and "IN catch" is
printed. The next line again throws an exception which is again
pushed. The same procedure is repeated and "I have really caught the
exception" is printed. Then in the end, the finally block is executed
which prints "This is finally" and since there are no exceptions
pending in the stack frame, the control continues with the method and
"After finally is printed".``
Now consider this snippet:
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println(1/0);
}
catch(Exception e)
{
System.out.println("In catch");
throw new NullPointerException();
}
finally
{
System.out.println("This is finally");
}
System.out.println("After finally");
}
}
``Here the same thing is repeated except for the fact that the stack
frame still has an exception pending and so after printing "This is
finally" the control returns to the calling method with the exception
still in store.
Its kind of funny why the "After finally" is not printed. Maybe
because the exception still remains on the stack frame and it is
seeking for someone to handle or for the exception to get propagated
to the main method and let the program die.``
Please be picky in evaluating my excerpt by pointing to the smallest
error. Any links to related articles would be helpful.
Thanks and regards,
S T S
exceptions I have come to some conclusions, which I would like to get
evaluated.
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println(1/0);
}
catch(Exception e)
{
try
{
System.out.println("In catch");
throw new NullPointerException();
}
catch(Exception ee)
{
System.out.println("I have really caught the
exception");
}
}
finally
{
System.out.println("This is finally");
}
System.out.println("After finally");
}
}
``Now my understanding is this. The first line causes an Arithmetic
Exception(divide by zero). This exception is pushed on to the current
stack frame of the executing method. The C runtime of the JVM now
searches for the corresponding catch block and finds one. Hence the
exception just pushed is popped off the frame and "IN catch" is
printed. The next line again throws an exception which is again
pushed. The same procedure is repeated and "I have really caught the
exception" is printed. Then in the end, the finally block is executed
which prints "This is finally" and since there are no exceptions
pending in the stack frame, the control continues with the method and
"After finally is printed".``
Now consider this snippet:
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println(1/0);
}
catch(Exception e)
{
System.out.println("In catch");
throw new NullPointerException();
}
finally
{
System.out.println("This is finally");
}
System.out.println("After finally");
}
}
``Here the same thing is repeated except for the fact that the stack
frame still has an exception pending and so after printing "This is
finally" the control returns to the calling method with the exception
still in store.
Its kind of funny why the "After finally" is not printed. Maybe
because the exception still remains on the stack frame and it is
seeking for someone to handle or for the exception to get propagated
to the main method and let the program die.``
Please be picky in evaluating my excerpt by pointing to the smallest
error. Any links to related articles would be helpful.
Thanks and regards,
S T S