permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception propagation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception propagation" Watch "Exception propagation" New topic
Author

Exception propagation

timo corn
Ranch Hand

Joined: Oct 26, 2013
Posts: 68

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.


hth
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

Consider this

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


Oracle Java Programmer , Oracle SQL Expert
timo corn
Ranch Hand

Joined: Oct 26, 2013
Posts: 68

In your example there is no propagation, if in method 2 there is an exception it might be catched?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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).


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

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.
 
Consider Paul's rocket mass heater.
 
subject: Exception propagation
 
Similar Threads
Exception
Will return something after error is thrown and finally executed?
RR#68 & #69
Confusion on when to use Try-Finally combination instead of Try-catch or Try-catch-finally?
Exception Propagation