Search Terms
throw, throwing non-error
Suggestion
Add typechecking to throw statement, allowing it to only throw Errors and anys.
Code currently allowed, but with strong potential for bugs:
throw null;
throw undefined;
throw 'foobar';
throw '';
throw 42;
throw {foo: 'bar'};
throw potentiallyNullable; // with –strictNullChecks
After implementing this feature request, such code wouldn't be allowed.
Use Cases
Even if it's possible to throw non-errors in JavaScript code, it's extremely confusing and hard to debug. Failing to properly handle such cases happened even to Mozilla.
Examples
Forbidden code:
throw null;
throw undefined;
throw 'foobar';
throw 42;
throw {foo: 'bar'};
throw potentiallyNullable; // with –strictNullChecks
Allowed:
throw new Error();
throw null as any;
Checklist
My suggestion meets these guidelines:
Search Terms
throw, throwing non-error
Suggestion
Add typechecking to
throwstatement, allowing it to only throwErrors andanys.Code currently allowed, but with strong potential for bugs:
After implementing this feature request, such code wouldn't be allowed.
Use Cases
Even if it's possible to throw non-errors in JavaScript code, it's extremely confusing and hard to debug. Failing to properly handle such cases happened even to Mozilla.
Examples
Forbidden code:
Allowed:
Checklist
My suggestion meets these guidelines: