Typescript: Make catch clause variables optional

Created on 28 Jul 2017  ·  7Comments  ·  Source: microsoft/TypeScript

The proposal by @michaelficarra makes the binding identifier in catch clauses optional.

For example:

try {
    JSON.parse("...");
}
catch {
    // Don't care, the error message is completely useless,
    // but we need to handle it.
}

This proposal reached stage 3 immediately upon presentation.

Committed ES Next Fixed Suggestion

Most helpful comment

I'd vote 🤷‍♀️, with further downleveling as appropriate.

All 7 comments

🚲 🏠 what should we name the variable when downleveling?

I was thinking unexpectedTokenInJsonAtPositionError.

I'd vote 🤷‍♀️, with further downleveling as appropriate.

The page is moved to https://tc39.github.io/proposal-optional-catch-binding/, for someone who wants to track the change of TS 2.5.

Just a thought but how about having the catch block itself as optional?

For example we have the 'traditional' try catch:

try {
    // Something risky
} catch (e) {
    // Handle the specifics of e
}
// Continue

The 'I don't care what went wrong' try catch:

try {
    // Something risky
} catch {
    // ¯\_(ツ)_/¯, Something happen lets do this instead
}
// Continue

So how about a 'I don't care' try (catch):

try {
    // Something risky
}
// Continue

That would be a good suggestion for ESDiscuss and I am sure that TypeScript would follow suit if the proposal made it. Diverging outside of that just in TypeScript wouldn't likely make sense.

@ZanderBrown try doesn't imply catch. Consider try...finally.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmaurer picture wmaurer  ·  3Comments

siddjain picture siddjain  ·  3Comments

dlaberge picture dlaberge  ·  3Comments

seanzer picture seanzer  ·  3Comments

manekinekko picture manekinekko  ·  3Comments