The document proposes a new syntax for catch clauses with guards for better code readability:
try {
someFunction();
} catch ErrorA (err) {
// Do something
} catch ErrorB | ErrorC (err) {
// Do another thing
} catch {
// Do a third thing
} finally {
// Clean up
}
The proposal is in need of a champion and a polished changeset to the spec.
ljharb
April 22, 2023, 8:15pm
2
This is commonly called catch guards, and there’s an existing proposal that’s planning to follow up pattern matching.
Hi @ljharb , @senocular ,
thanks for your comments. I have updated the proposal accordingly and added a section that compares the document with other proposals.
ljharb
April 23, 2023, 11:48pm
5
I wouldn’t support anything that has instanceof semantics, because instanceof is not reliable or robust.
That would be more in line with typescript, but not javascript because | is the binary OR operator. Instead you can see a proposal closer to the ECMA specifications
Universal and conditional catch blocks with the new when keyword