File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Debug issue for return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Debug issue for return" Watch "Exception Debug issue for return" New topic

Exception Debug issue for return

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 423
Hi, In the first example, control is not coming to return statement after finally block. But, In second example, control is coming to return statement as expected after finally block execution. Please advice if you know the reason.

OCPJP 6.0-81% | Preparing for OCWCD | |
Jigar Gandhi

Joined: May 13, 2008
Posts: 20
In the first statement there is a DivideByZero Error so both catch and finally block gets executed.
while in the second case there is no exception so it returns and then executes the finally.
Finally is a block that is executed compulsarily after the try block.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
in the first example an exception occurs at line 05 (Divide by zero).
In case of exception Java stops executing the try block
- the remaining instructions in the try block (after the point where exception has ocurred) are simply skipped - Java does not execute them.
Java executes an appriotriate catch block (if any), then finally block (if exists), and finishes executing the whole try-catch-finally block.
Java does not retry the instruction where exception has ocurred not continue executing instructions that are below this point.
Read java tutorial to refresh basics about exception handling:
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 423
Your explanation is much better than documentation..Thanks Ireneusz and Jigar.
I agree. Here's the link:
subject: Exception Debug issue for return
It's not a secret anymore!