• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

return in try/catch block

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[])
{
try
{
int i;
return;
}catch(Exception e)
{
System.out.println("Hi everybody!");
}finally
{
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.

try{
URL u=new URL(path);//assume path is a previously defined String
Object obj=in.readObject();// in is a valid ObjectInputStream
System.out.println("Success");
}
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");
}
finally{
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 .
--Shallender
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Waiting for the reply.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Regds
Sathish
 
What's a year in metric? Do you know this metric stuff tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic