Somehow this does not feel like the culmination of the 50 years programming language development:
throw "My exception message here";
What’s the correct way to do exceptions in Javascript, so that
-
They can be identified (instanceof)
-
They can carry other payload besides the default message and stack trace
-
They “subclass” base Exception, so that debug console and such can extract meaningful information about the exception
-
Possible nested exceptions (converting exception to another): if you need to catch an exception and rethrow new one the orignal stack trace would be preserved and could be meaningfully read by debugging tools
-
They follow Javascript best practices
throw new Error("message");or if you want to be more specific use one of the Error Objects
It’s important to make sure you throw real errors because they contain the stack trace. Throwing a string is stupid because it doesn’t have any meta data attached to it.
You can also subclass errors