Repository navigation
[Question] Try catch support #302
Description
Activity
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.Reacted by Steve, Matt Johnson-Pint and @nreiThanks for the clarification :)
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.
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?
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
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).Reacted by Matt Johnson-PintI see. So what's the recommended way of handling non-fatal exceptions?
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 { ... }
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 }; } }
Reacted by Bowen Wang, Shmuel Disraeli, Joe Lapp and Peter Batory-BernardinUnfortunately, 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.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?
Reacted by Luffy Cheng, Ryan Noble, Adrien Bibollet, Víctor Hernández, Matt Johnson-Pint, Taine Zhao, sophie and Matus NovakReacted by Michael Westcott, Adam Wootton, Johannes Fritsch, Carlos Fdez. Llamas, Guilherme Moraes and Shane LiesegangReacted by Carlos Fdez. Llamas and Stefano Ceschi BerriniReopen this issue, there's still no try/catch support.
Essentially,
throwin AssemblyScript is not at all likethrowin TypeScript or JavaScript. It's more likepanicin Go, and there is no equivalent of Go'srecover.
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?