[Question] Try catch support #302
Copy link
Copy link
Closed
Description
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?
Reactions are currently unavailable
Activity
dcodeIO commentedon Oct 16, 2018
Unfortunately, the exception-handling spec hasn't landed yet and we are waiting for it as well. What AS does currently is converting
throwstatements to a anabort(...); unreachable();(simply put), which cannot be caught.t57ser commentedon Oct 16, 2018
Thanks for the clarification :)
stale commentedon Feb 8, 2019
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.
bowenwang1996 commentedon May 13, 2019
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?
MaxGraey commentedon May 13, 2019
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
dcodeIO commentedon May 13, 2019
Background: Doing
throw new Error("something")currently compiles to anunreachableinstruction that cannot be safely recovered from. You can catch anunreachableon 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).bowenwang1996 commentedon May 13, 2019
I see. So what's the recommended way of handling non-fatal exceptions?
dcodeIO commentedon May 13, 2019
So far I've used return values to indicate the outcome of an operation that might fail. Fanciest one can do probably is
MaxGraey commentedon May 13, 2019
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:
thebakersteve commentedon Feb 13, 2022
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?
fucksophie commentedon Mar 1, 2025
Reopen this issue, there's still no try/catch support.
mattjohnsonpint commentedon Mar 2, 2025
Essentially,
throwin AssemblyScript is not at all likethrowin TypeScript or JavaScript. It's more likepanicin Go, and there is no equivalent of Go'srecover.