Are try catch statements supported?

I am writing an extension with useful utility blocks. Two of them are try and try-catch blocks, but it seems like they don’t work when a block that causes errors is put in.
Here’s the code for the try-catch block, another issue I have is that I can’t have multiple “mouths” (where you can put code in):

export function tryCatchCode(tryCode: () => void, catchCode: (errorName: string) => void): void {
    try {
        tryCode();
    } catch (error) {
        catchCode(error.name);
    }
}

Is it possible to have two “mouths” in a single block and are try statements supported?

Thought it was because it can’t bypass memory errors, but even with casting on null the try statement still fails and crashes. Doing try statements plainly on JavaScript code does work but as a block in an extension it doesn’t.

I think blocks might not support try-catch statements. The doc I linked in another thread refers to the JavaScript / TS features implemented

1 Like

Good questions!

  1. Currently we don’t have authoring to support multiple function handlers as parameters (to make something like an if / else block) but it’s something we can consider – I’ve wanted it at times before but never enough to actually make it work :slight_smile:
  2. It looks like try / catch is only working with explicit throw "abc" calls at the moment, as far as I can tell – which would make this make sense with null pointer exceptions being the most common error you’ll likely hit. I filed this as an issue https://github.com/microsoft/pxt-arcade/issues/5852 so it can be discussed more over there~
3 Likes
  1. Where can I suggest a suggestion on the GitHub of Arcade? There doesn’t seem to be a suggest tag on the issues tab?
  2. (Can’t log in to GitHub right now, sorry about that!) Wouldn’t it be dangerous to still try and catch errors that’s about the memory? Also just to be sure, I’m talking about running the code in the function handler (“Mouth’s”)in try statements in extensions.

Thank you for answering!

  1. Just file an issue, I believe it’ll give an option for bug report or enhancement (if so, just choose enhancement) - one of the members of our team will triage it when we get time and give it appropriate tags
  2. Maybe! I mentioned in the issue that it should either be supported or documented that it’s not- I haven’t looked at the code that handles it recently enough to know how hard it’d be to support. And yes, that’s what I meant as well - right now our block logic checks for event handles and adds a ‘mouth’ if there is a function as a parameter, this would be theoretically adding support for two or more functions as parameters. Have to double check, we might have an issue for that already otherwise i can file one on monday~
2 Likes

I went ahead and filed the request for multiple handler parameters here, I don’t know when we’ll get the chance to make it work but if anything changes that issue will be updated accordingly :slight_smile:

2 Likes