• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

try/catch??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Options:
1)It will not compile
2)It will throw an exception at Runtime
3)It will print 800
4)It will print 400
5)It will print 200
Ans:4)It will print 400
I feel the answer should be 5)200, since the exception is caught in the catch block which returns 3 and thus amount =100*2 i.e 200
Please explain..
I would like to ask incase in the switch statement, the case 3 is caught, will it still go further for other cases like case 7 or 6 or default?
Sonir
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in try/catch/finally block, finally will execute even if an exception is caught in catch, so it will return 7.
in switch, as there's no break after case, all the statements after the matching case will also be executed, until it meets a break or the end of the switch. therefore case 7: amount = 100 * 2 = 200, case 6: amount = 200 + 200 = 400.
try to use other numbers. you'll find that for the numbers not greater than 10, the result is always 400 because of the finally block.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this part is not understood....
catch(Exception e)
{return 3;}
finally
{return 7;}
say case is that Exception IS generated. so catch and finally both will be executed. So as far as the method is concerned what is returned ? 3 or 7 ?
(this question is more about the fundamental of return concept)

Originally posted by patrick tang:
in try/catch/finally block, finally will execute even if an exception is caught in catch, so it will return 7.
in switch, as there's no break after case, all the statements after the matching case will also be executed, until it meets a break or the end of the switch. therefore case 7: amount = 100 * 2 = 200, case 6: amount = 200 + 200 = 400.
try to use other numbers. you'll find that for the numbers not greater than 10, the result is always 400 because of the finally block.

 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally , if present, ALWAYS runs, whether or not any catch blocks run.
finally basically has the "last say" about how a method returns control to the caller of that method.
In a simple case, finally just tidies up some allocated system resources, such as an open file.
OutputStream os;
try
{
//something to cause exception
}
catch(IOException fne)
{
//print some error message
}
finally
{
os.close();
}
This is the simple case. Either the try block completes successfully, or an exception is raised and control passes to the catch block. Regardless, the finally will run and the OutputStream gets closed.
Now, since finally ALWAYS runs, what happens if you have a try/catch like this:

what will this method return? Remember that finally runs, and it runs right after the last statement in the catch(), HOWEVER, it can't run after the "return" statement, because this would cause control to leave this method. So it runs *before* the return.
X gets set to 20 in the finally, then control passes back to the catch, and the return gets executed.
If you write up a few test programs you can see how try/catch/finally work together.
Rob
[ January 13, 2002: Message edited by: Rob Ross ]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally
{ x = 20; }
return x;
Rob, actually due to the return statement after the finally block, this will not be allowed to compile. the return after finally has to be removed. because finally is the last to be executed before the program passes out of this block and return x would never be reached.

Originally posted by Rob Ross:
finally , if present, ALWAYS runs, whether or not any catch blocks run.
finally basically has the "last say" about how a method returns control to the caller of that method.
In a simple case, finally just tidies up some allocated system resources, such as an open file.
OutputStream os;
try
{
//something to cause exception
}
catch(IOException fne)
{
//print some error message
}
finally
{
os.close();
}
This is the simple case. Either the try block completes successfully, or an exception is raised and control passes to the catch block. Regardless, the finally will run and the OutputStream gets closed.
Now, since finally ALWAYS runs, what happens if you have a try/catch like this:

what will this method return? Remember that finally runs, and it runs right after the last statement in the catch(), HOWEVER, it can't run after the "return" statement, because this would cause control to leave this method. So it runs *before* the return.
X gets set to 20 in the finally, then control passes back to the catch, and the return gets executed.
If you write up a few test programs you can see how try/catch/finally work together.
Rob
[ January 13, 2002: Message edited by: Rob Ross ]

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try/catch/finally and return... boy that's a confusing combo..
treat each of them (try/catch/finally) as a seperate block...
Always finally return stmt overrides the other returns
If there are any return stmts after
finally{ return 0;} then it is a compile time error..
Ragu
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:
finally
{ x = 20; }
return x;
Rob, actually due to the return statement after the finally block, this will not be allowed to compile. the return after finally has to be removed. because finally is the last to be executed before the program passes out of this block and return x would never be reached.




Actually Mark, without the last return statement there, the method won't compile. The method declaration states it is returning an int. If the try block completes successfully, and no exceptions are raised, controll will pass to the finally block, then to the block immediately following the try/catch/finally. At this point, the method ends. If you don't return an int, the compiler will complain that you're not honoring your declaration.
Try to compile it each way and see for yourself.
Rob
[ January 13, 2002: Message edited by: Rob Ross ]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I tried to run thos piece of code, and it ran well
with return statement at the end of get(). BTW, I
am using jdk 1.2.
victor

Originally posted by Ragu Sivaraman:
try/catch/finally and return... boy that's a confusing combo..
treat each of them (try/catch/finally) as a seperate block...
Always finally return stmt overrides the other returns
If there are any return stmts after
finally{ return 0;} then it is a compile time error..
Ragu

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by victor gu:
hi,
I tried to run thos piece of code, and it ran well
with return statement at the end of get(). BTW, I
am using jdk 1.2.
victor


victor...
I am not sure which code you ran ...
But here is a thought
If you dont have return stmts in either one of try/catch then the return stmt after the try/catch/finally will be called to return back to the caller.. But it will only work
if the finally doesnt have a return stmt. If not it wont compile
Hope it helps you
Here try this..
<CODE>
class Exceptiontest {
public static void main(String args[]) {
Exceptiontest e = new Exceptiontest();
System.out.println(e.foo());
}
public int foo() {
try{
File f = new File("A.txt");
System.out.println(f.toURL());
//int of = 1/0;
System.out.println("Ragu");
//return 1;
}catch(Exception e) {
System.out.println("Java");
return 2;
}
finally{
//return 3;
}
return 4;
}
</CODE>
If you play around with those return stmts you will know
Have fun
Ragu
 
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
Ragu:
Yes, if we put a return statement in finally block, then the one at the end of get() cannot
be reached. Otherwisem it runs fine. Sorry for
misreading your reply

victor

Originally posted by Ragu Sivaraman:

victor...
I am not sure which code you ran ...
But here is a thought
If you dont have return stmts in either one of try/catch then the return stmt after the try/catch/finally will be called to return back to the caller.. But it will only work
if the finally doesnt have a return stmt. If not it wont compile
Hope it helps you
Here try this..
<CODE>
class Exceptiontest {
public static void main(String args[]) {
Exceptiontest e = new Exceptiontest();
System.out.println(e.foo());
}
public int foo() {
try{
File f = new File("A.txt");
System.out.println(f.toURL());
//int of = 1/0;
System.out.println("Ragu");
//return 1;
}catch(Exception e) {
System.out.println("Java");
return 2;
}
finally{
//return 3;
}
return 4;
}
</CODE>
If you play around with those return stmts you will know
Have fun
Ragu

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys..
In the end, one thing is for sure i.e
a finally block should have a return statement orelse it wont compile??
Is this right??
Sonir
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No sonir. The original example program we have been talking about contained a return in the finally, and this meant that the exit point of the method was modified.
finally doesn't have to include a return, any more than a try or a catch block has to have a return; it depends on what the method is trying to accomplish;
Although, I would say it's "messy programming" to include a return statement in a finally block. But as a programmer, you should be able to predict the effect of such things just in case.
Rob
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
do you mean to say ,that following code will compile if there is no Exception.

When I tried this there is a compile time error at return;
Is it return; or return x; ?
-Tony
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic