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.
Rob
SCJP 1.4
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 ]
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.
Rob
SCJP 1.4
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
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
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
Rob
SCJP 1.4
Don't get me started about those stupid light bulbs. |