No - at the IL level you can't return from inside an exception-handled block. It essentially stores it in a variable and returns afterwards
i.e. similar to:
int tmp;
try {
tmp = ...
} finally {
...
}
return tmp;
for example (using reflector):
static int Test() {
try {
return SomeNumber();
} finally {
Foo();
}
}
compiles to:
.method private hidebysig static int32 Test() cil managed
{
.maxstack 1
.locals init (
[0] int32 CS$1$0000)
L_0000: call int32 Program::SomeNumber()
L_0005: stloc.0
L_0006: leave.s L_000e
L_0008: call void Program::Foo()
L_000d: endfinally
L_000e: ldloc.0
L_000f: ret
.try L_0000 to L_0008 finally handler L_0008 to L_000e
}
This basically declares a local variable (CS$1$0000
), places the value into the variable (inside the handled block), then after exiting the block loads the variable, then returns it. Reflector renders this as:
private static int Test()
{
int CS$1$0000;
try
{
CS$1$0000 = SomeNumber();
}
finally
{
Foo();
}
return CS$1$0000;
}