4 ms·
Just tested that in C# and it seems they made the smart decision to not allow shenanigans like that in a finally block: CS0157 Control cannot leave the body of
by ziml77 1y ago
Just tested that in C# and it seems they made the smart decision to not allow shenanigans like that in a finally block:
CS0157 Control cannot leave the body of a finally clause
- o11c 1y agoWhat about throwing an exception from the finally clause?
- messe 1y agoThis loops, if that's what you're asking: while (true) { try { try { return; } finally { throw new Exception(); } } catch { } }
- ziml77 1y agoYes that is the one exception (heh) to the rule unfortunately. You can throw from anywhere so it must support unwinding from any point. So if you were really intent on abusing the finally you could wrap the try-finally in a try-catch and then throw from the finally and put your continue in the catch.
- kant2002 1y agoThe finally behave slightly different in CIL. You have protected regions and finally/fault/catch/filters handlers attached. So in order to support continue inside finally you should introduce some state machine , which is complication and generally against Roslyn design limitation.
- MaxBarraclough 1y agoziml77's point is about the behaviour of the C# language. You seem to be talking about implementation concerns. They're not relevant.