This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return in try/catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return in try/catch block" Watch "return in try/catch block" New topic

return in try/catch block

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am putting two questions in which we have return statement in one question after try block and in the second question it is after catch block. Please explain me what the return does in try/catch block.
Question: 1
What is the O/P of the following try...catch block

public class fun
public static void main(String args[])
int i;
}catch(Exception e)
System.out.println("Hi everybody!");
System.out.println("We love Java");
a. Hi everbody!
b. We love Java
c. No Output
d. Depends on the creation of int i
Answer: b
Question : 2
Consider the following code fragments.

URL u=new URL(path);//assume path is a previously defined String
Object obj=in.readObject();// in is a valid ObjectInputStream
catch(MalformedURLException e){
System.out.println("Bad URL");
catch(StreamCorruptedException e){
System.out.println("Bad File Contents");
catch(Exception e){
System.out.println("General Exception");
System.out.println("Finally Part");
System.out.println("Carrying On");
What lines are output if the code at the second line throws a MalformedURLException. Choose all valid answers.
a. Success
b. Bad URL
c. Bad File Contents
d. General Exception
e. Finally part
f. Carrying On
Ans: b,e and f

Please expalin .
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Waiting for the reply.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
1) the finally block is always executed except an exit() method was encounted while executing the try block and the program complete abruptly.
2) if an exception is thrown during executing the try block, the program control transfered to search the exception handler and execute it if an compitible handler existed.
3) if exception handled properly, the program will continue to execute normally.

Joined: Sep 07, 2000
Posts: 16
Hi Shallender,
I had seen a similar qn at another place and they had warned me to be careful abt it; allow me to pass on the piece of advice to you.
As you might already know, the finally block is always executed whether an exception was raised or not. The only situation when the finally block is not executed is when the VM stops within a try or catch block. i.e. unless u do a System.exit() within a try or a catch block, the finally block is definitely executed.
Even though there is a return statement within the try block, the finally block is executed. Once (say) 'return someValue;' is executed, 'someValue' is returned to the calling statement only after the finally block is executed. Or as in your case the method simply returns after printing out "We love Java" of the finally block. And hence the answer that u get.
I hope I've been able to help.
I agree. Here's the link:
subject: return in try/catch block
It's not a secret anymore!