Runtime errors in JavaScript are instances of the Error
object. The Error
object can also be used as-is, or as the base for user-defined exceptions. It's possible to throw any type of value - for example, strings - but you're strongly encouraged to use Error
or one of it's derivatives to ensure that debugging information -- such as stack traces -- is correctly preserved.
The first parameter to the Error
constructor is the human-readable error message. You should try to always specify a useful error message of what went wrong, even if additional information can be found elsewhere.
try {
throw new Error('Useful message');
} catch (error) {
console.log('Something went wrong! ' + error.message);
}