exception-handling
exception-handling copied to clipboard
Should there be support for a finally clause?
Some languages such as Java and Python support a finally clause with a definition like that from the Python spec:
A finally clause is always executed before leaving the try statement, whether an exception has occurred or not.
Finally can be expressed with catch and (re)throw, so isn't required as a primitive. Compilers tend to compile it away (e.g. LLVM, AFAIK).
I agree with @rossberg-chromium that one can implement the concept, but it requires an "artificial" throw in the try body.
I think a better solution is to define a second
try inst* finally inst* end
block that always evaluates the finally instructions. This cleanly separates the two types of semantics, keeping things simple.
I might be totally wrong here, but it seems to me that wasm
is an intermediate representation of compiler output. As such, it will be used to combine separately compiled entities from different compilers. Therefore, it would seem better provide to the notion of finally
as a primitive so the semantics of the finally
clause can be unambiguously communicated.
@winksaville, not quite, Wasm is an abstraction defining a virtual ISA. Automatic interoperability between different compilers is explicitly a non-goal (they would need to collaboratively define a common ABI).
@rossberg-chromium, it seems to me that finally
has very definite semantics that is intertwined with the try/catch/else implementation and semantics. So defining it as @KarlSchimpf proposes above or some other way would be beneficial even within a system that's built by a single compilation.
Would it be ok to consider a try inst* finally' inst* end
(as I suggested above) to be a future feature? Or, do you feel that this must be baked in this initial, minimal proposal?
IMHO it should be in the MVP as try finally
is integral to several languages, but it's your call.
Question, are you sure try/finally
can be implemented in terms of try/catch
since wasm
is limited to structured control flow?
Also, reading exception handing on wikipedia there seems to be at least a implementation schemes, dynamic registration and table driven. Have you implemented anything yet?
SG and I think your suggestion of separating try finally
from try catch
is a good one.
Marked as (future) enhancement to this proposal.
Out of curiosity, I looked at the IL generated from compiling the following C# script:
try {
...
} catch {
...
} finally {
...
}
The IL byte code that the compiler generated looked like this (ish):
try {
try {
...
} catch {
...
}
} finally {
...
}
So it seems like there may be something to this idea of separating the try catch
and the try finally
blocks.
One additional issue with finally is that it gets called also when within the try block a 'return' call is made.
@bartwe, in Wasm, return
is just a special case of a br
, where the same applies. The same semantics exists in e.g. C++, yet, compilers like clang on LLVM compile away finally
.
Should this be closed in favor of #158?
Yeah I think this can be closed. I don't think we will have finally
in the current version of the EH proposal anyway though.