Skip to content

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

Source

See also  Java code snippet - How to make jframe visible?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.