logoalt Hacker News

AlienRobotlast Monday at 10:21 AM2 repliesview on HN

What if...

    try {
       let x = parseInt(input);
       and {
           displayResult(x / 0);
       }
    } catch {
       displayError("Parsing error.")
    }
And the catch can't catch a division by zero error because it occurred inside an "and" block.

Replies

sillysaurusxlast Monday at 1:23 PM

In practice you can move the displayResult(x / 0) outside the try catch. It’s hard to think of a counterexample that comes up in practice.

There might be something to this idea, because I’ve often been in situations where a try catch catches an error that originates a dozen frames down the call stack, which is sometimes (but not always) unrelated to the original point of the try catch block. People try and deal with this by adding more try catches around the code deeper in the call stack, but maybe there’s a better way.

rcxdudelast Monday at 12:11 PM

That's kind of like python's try/else, but not quite (the 'and' would be more general, if you can interleave them with statements in the try block)