'finally' block

B

Brian

Is it possible to have more than one try/catch/finally
block within a method? Also, if the code never enters the
try block of code, is the finally block executed? I have
a try block in a branch of a switch statement and I only
want the finally to execute if the branch of the switch
statement is executed.
 
J

james

Yes you can have multiple t/c/f blocks in one method.
The finally will not be executed if you return before your
try or condition around it. As for your switch logic, I'd have to see
the code, there are differnet possibilities

JIM
 
T

Tu-Thach

you can have as many try/catch/finally block in a method
as you like. The finally block only executes when the
corresponding try block was executed.

Tu-Thach
 
H

Hermit Dave

you can have

try
{
.......
.......
}
catch(C1)
{
......
}
catch(C2)
{
.......
}
finally
{
}

yes you can have the entire block within a switch brach... but then make
sure the catch if you include any and the finally if you add that as well
are within the same branch
if you want a global try catch finally
try placing calling method within try block and catch the exception and your
finally block

try
{
callFuncitonWithSwitch()
}
catch(c1)
{
}
catch(c2)
{
}
finally
{
}

the finally block should be associated with a corresponding try block... so
you cant have a try at one place and finally at a totally irrelevant
place...
and finally if the code execution never enters try... it never goes through
the finally either...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top