Throwing exceptions with specific values in JavaScript
In JavaScript, throw statement throws a user-defined exception. It can be an expression, values of primitive types or Objects. Here are a few examples.
Advertisements
// throw an exception, expression specifies the value of the exception. eg: throw 'Error2'; // generates an exception with a string value throw 42; // generates an exception with the value 42 throw true; // generates an exception with the value true throw new Error('Required'); // generates an error object with the message of Required