Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Should there be support for a finally clause? #11

Closed

Description

@winksaville

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.

Activity

  1. rossberg commented on May 11, 2017

    @rossberg
    Member

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

  2. KarlSchimpf commented on May 11, 2017

    @KarlSchimpf
    Contributor

    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.

  3. winksaville commented on May 11, 2017

    @winksaville
    Author

    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.

  4. rossberg commented on May 11, 2017

    @rossberg
    Member

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

  5. winksaville commented on May 11, 2017

    @winksaville
    Author

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

  6. KarlSchimpf commented on May 12, 2017

    @KarlSchimpf
    Contributor

    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?

  7. winksaville commented on May 12, 2017

    @winksaville
    Author

    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?

  8. winksaville commented on May 12, 2017

    @winksaville
    Author

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

  9. KarlSchimpf commented on May 15, 2017

    @KarlSchimpf
    Contributor

    Marked as (future) enhancement to this proposal.

  10. Abion47 commented on May 15, 2017

    @Abion47

    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.

  11. bartwe commented on Jun 2, 2017

    @bartwe

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

  12. rossberg commented on Jun 2, 2017

    @rossberg
    Member

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

  13. osa1 commented on Oct 9, 2022

    @osa1
    Contributor

    Should this be closed in favor of #158?

  14. aheejin commented on Oct 9, 2022

    @aheejin
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions