Skip to content

[Question] Try catch support #302

Description

@t57ser

Is there any support for using try/catch in AS?
Currently the compiler says "Operation not supported".
I've found this old issue: https://github.com/AssemblyScript/prototype/issues/103
Though when looking at the webassembly link: https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md
it is talking about try/catch blocks. What's the state for it in Assemblyscript?

Activity

  1. dcodeIO commented on Oct 16, 2018

    @dcodeIO
    Member

    Unfortunately, the exception-handling spec hasn't landed yet and we are waiting for it as well. What AS does currently is converting throw statements to a an abort(...); unreachable(); (simply put), which cannot be caught.

  2. t57ser commented on Oct 16, 2018

    @t57ser
    Author

    Thanks for the clarification :)

  3. stale commented on Feb 8, 2019

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  4. bowenwang1996 commented on May 13, 2019

    @bowenwang1996
    Contributor

    Does that mean we cannot detect whether an exception occurred? For example, if I want to write a test and assert that an exception has occurred, is there anyway to do that?

  5. MaxGraey commented on May 13, 2019

    @MaxGraey
    Member

    You could throw exceptions but can't catch or rethrow it currently without interrupt module's execution. Current WebAssembly MVP cupport only global traps but this feature extension will allow support fully featured try/catch/finally in near future

  6. dcodeIO commented on May 13, 2019

    @dcodeIO
    Member

    Background: Doing throw new Error("something") currently compiles to an unreachable instruction that cannot be safely recovered from. You can catch an unreachable on the JS side of course, but it's not safe because the internal state might be broken as soon as that occurs. Or, short: Don't use exceptions yet unless the exception is fatal and not meant to be caught (but to abort the program).

  7. bowenwang1996 commented on May 13, 2019

    @bowenwang1996
    Contributor

    I see. So what's the recommended way of handling non-fatal exceptions?

  8. dcodeIO commented on May 13, 2019

    @dcodeIO
    Member

    So far I've used return values to indicate the outcome of an operation that might fail. Fanciest one can do probably is

    enum Status {
      OK,
      ERROR_A,
      ERROR_B
    }
    
    function doSomething(): Status {
      ...
    }
  9. MaxGraey commented on May 13, 2019

    @MaxGraey
    Member

    Languages like Go and Rust haven't support exceptions and usually use Optional types or tuples / objects during return result. You could follow same approach:

    function parseArray<T>(str: string): T[] | null {
       if (isValid(str)) return parseArr(str)
       return null;
    }
    
    // or
    class Result<T> {
       value: T | null = null;
       error: string | null = null;
    }
    
    function getRequestResult(url: string): Result {
        let body = ...
        if (body == null) {
          return { value: null, error: "can't request from url" };
        } else {
          return { value: body, error: null };
        }
    }
  10. thebakersteve commented on Feb 13, 2022

    @thebakersteve

    Unfortunately, the exception-handling spec hasn't landed yet and we are waiting for it as well. What AS does currently is converting throw statements to a an abort(...); unreachable(); (simply put), which cannot be caught.

    It's so funny that I'm stuck because the language doesn't have the most basic exception handling. Do you get that? your language can't do try-catch..why work on anything else other than this?

  11. fucksophie commented on Mar 1, 2025

    @fucksophie

    Reopen this issue, there's still no try/catch support.

  12. mattjohnsonpint commented on Mar 2, 2025

    @mattjohnsonpint
    Contributor

    Essentially, throw in AssemblyScript is not at all like throw in TypeScript or JavaScript. It's more like panic in Go, and there is no equivalent of Go's recover.

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