A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
return statement in try block
rajesh kumar rayavarapu
Joined: May 16, 2007
May 29, 2007 13:16:00
Hi iam beginer to the
npls tell me the answer i have faced this question in on of the interview.
In the above program i have written return statement in try block,if exception is there in try block what happend(return is there in try block) and exception is not there in try block?
pls tell me i have faced this question two interviews yaaa
Jesper de Jong
Joined: Aug 16, 2005
May 29, 2007 13:58:00
Write a small program yourself, compile it and run it to find out what happens. The best way to learn is to experiment.
Java Beginners FAQ
JavaRanch SCJP FAQ
The Java Tutorial
Java SE 7 API documentation
- My blog about Scala
Joined: May 29, 2007
May 30, 2007 13:41:00
If you will get an exception it will go to the catch and finally and if no exception then only finally.So return will not effect much.
Best way is write a code and run all aspects,the questions in your mind.
Joined: Apr 18, 2007
May 30, 2007 14:47:00
Here's the thing that you should burn in when it comes to TRY and CATCH.
1. No matter whether an exception is thrown or not, finally block will be executed!!!.
2. If an exception is thrown, return will not be executed, catch and then finally both will be executed.
Joined: Nov 18, 2006
Jun 20, 2007 00:14:00
I believe that the only way to prevent a finally-block from being executed is to call the method:
The fact that this prevents finally from being called may be a reason to throw an exception or an error instead of calling System.exit();
I agree. Here's the link:
subject: return statement in try block
Can we have return statement in try block ?
Little confusion in Finally block
Try an Finally Working Confusion
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014