exception-handling icon indicating copy to clipboard operation
exception-handling copied to clipboard

Should there be support for a finally clause?

Open winksaville opened this issue 7 years ago • 12 comments

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.

winksaville avatar May 11 '17 18:05 winksaville

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).

rossberg avatar May 11 '17 18:05 rossberg

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.

KarlSchimpf avatar May 11 '17 18:05 KarlSchimpf

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 avatar May 11 '17 18:05 winksaville

@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 avatar May 11 '17 19:05 rossberg

@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.

winksaville avatar May 11 '17 21:05 winksaville

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?

KarlSchimpf avatar May 12 '17 16:05 KarlSchimpf

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?

winksaville avatar May 12 '17 17:05 winksaville

SG and I think your suggestion of separating try finally from try catch is a good one.

winksaville avatar May 12 '17 19:05 winksaville

Marked as (future) enhancement to this proposal.

KarlSchimpf avatar May 15 '17 15:05 KarlSchimpf

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.

Abion47 avatar May 15 '17 19:05 Abion47

One additional issue with finally is that it gets called also when within the try block a 'return' call is made.

bartwe avatar Jun 02 '17 11:06 bartwe

@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.

rossberg avatar Jun 02 '17 12:06 rossberg

Should this be closed in favor of #158?

osa1 avatar Oct 09 '22 04:10 osa1

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.

aheejin avatar Oct 09 '22 21:10 aheejin