Could you explain in some sentences what the "propagation of the exception back to the try block" mean if I have some methods on a call stack(exception thrown in one method) and a try catch block that calls the first method.
I got this info:
Exceptions propagate up through block structure of a Java method, and then up the method call stack. If an exception is not caught by the block of code that throws it, it propagates to the next higher enclosing block of code. If it is not caught there, it propagates up again. If it is not caught anywhere in the method, it propagates to the invoking method, where it again propagates through the block structure. If an exception is never caught, it propagates all the way to the main() method from which the program started, and causes the Java interpreter to print an error message and a stack trace and exit.
So call stack will be main at bottom , then method1 ,then method2 .
If while executing code in method2 we get an Exception either it will be handled by proper catch block or it will propagate down to the hierarchy of code execution.
Now suppose while creating a database connection we get an Exception and ExceptionType a can not catch it then it will propagate to ExceptionType b if even that can not catch it
it will propagate to method1 which called method2 now worst If even method1 can not handle the Exception then it will propagate to main method, Since main called the method1 and from there it all started.
Now if main can not handle this Exception then you will get Exception at runtime.
Thats how Exceptions propagate I hope you understand it
timo corn wrote:In your example there is no propagation, if in method 2 there is an exception it might be catched?
In method2, if there is some other exception (other than 'ExceptionType') then it won't be handled, and it will be propagated all the way to main method; and since main method is not handling it either, it will throw it even above (i.e. out of the program and to JVM).
Yes If there is appropriate exception handler in method2 then it will be caught and handled and will not propagate , But if there is not an appropriate exception handler then exception will propagate in the manner that I wrote.