Since we can throw anything with the throw keyword in Javascript, can’t we just throw an error message string directly?
Does anyone know any catch in this?
Let me add some background to this: Very often, in the JavaScript world, people rely on parameter checking as opposed to using the try-catch mechanism, so it makes sense to only throw fatal errors with throw. Still, to be able to catch some system Errors, I have to use a different class for my own errors and instead of creating a subclass of Error, I think I should just use String.
While it is
okaypossible to throw any value, it is generally considered poor form to throw anything other than an instance ofErroror one of its subclasses. There are several reasons for this:message,stacktrace, andnameproperties that appear onErrors.