S
Sean Kirkpatrick
I got caught with my pants down the other day when trying to explain
Try...Catch...Finally and things didn't work as I had assumed. Perhaps
someone can explain to me the purpose of Finally. I've looked at several
texts that I have and none of them address this specific point.
If I call some method that throws an exception in my routine Foo,
sub foo
call bar <- throws an exception
do something else <- never get here
end sub
control is passed back up the call stack to the first valid catch block.
So far so good.
Now if I do this
sub foo
try
call bar (which throws an exception)
catch
handle the exception <- only if exception thrown
end try
do something else <- since exception handled, do this
end sub
execution goes to the catch block and then continues to do something
else. So far so good.
If I do this
sub foo
try
call bar (which throws an exception)
catch
handle the exception <- only if exception thrown
finally
always do this
end try
do something else <- since exception handled, do this
end sub
the Finally block ALWAYS gets executed regardless of an exception being
thrown. Ok.
I submit that the second case is identical to the third case if I
rewrite number 2 as follows:
sub foo
try
call bar (which throws an exception)
catch
handle the exception <- only if exception thrown
end try
always do this
do something else <- since exception handled, do this
end sub
This begs the question - what is the use of Finally? I had assumed,
wrongly as it turns out, that in the event of an exception, Catch and
Finally would execute and control would return to the caller. If no
exception, Finally would execute and control continues to the next
statement.
I can see no use for Finally!
Sean
Try...Catch...Finally and things didn't work as I had assumed. Perhaps
someone can explain to me the purpose of Finally. I've looked at several
texts that I have and none of them address this specific point.
If I call some method that throws an exception in my routine Foo,
sub foo
call bar <- throws an exception
do something else <- never get here
end sub
control is passed back up the call stack to the first valid catch block.
So far so good.
Now if I do this
sub foo
try
call bar (which throws an exception)
catch
handle the exception <- only if exception thrown
end try
do something else <- since exception handled, do this
end sub
execution goes to the catch block and then continues to do something
else. So far so good.
If I do this
sub foo
try
call bar (which throws an exception)
catch
handle the exception <- only if exception thrown
finally
always do this
end try
do something else <- since exception handled, do this
end sub
the Finally block ALWAYS gets executed regardless of an exception being
thrown. Ok.
I submit that the second case is identical to the third case if I
rewrite number 2 as follows:
sub foo
try
call bar (which throws an exception)
catch
handle the exception <- only if exception thrown
end try
always do this
do something else <- since exception handled, do this
end sub
This begs the question - what is the use of Finally? I had assumed,
wrongly as it turns out, that in the event of an exception, Catch and
Finally would execute and control would return to the caller. If no
exception, Finally would execute and control continues to the next
statement.
I can see no use for Finally!
Sean