JavaScript throw Statement with Example

JavaScript throw Statement with Example

In this tutorial, you will learn about JavaScript throw statements with the help of examples.

You learned how to handle exceptions in JavaScript using the try..catch statement in the last tutorial. The try-and-catch statements handle exceptions in the conventional JavaScript manner. However, the throw statement can be used to pass user-defined exceptions.

The throw statement in JavaScript handles user-defined exceptions. For example, if a given number is divided by 0, and you need to treat Infinity as an exception, you can use the toss statement.

Note: The throw statement causes a user-defined exception to be thrown. The current function’s execution will be terminated (the statements after the throw will not be performed), and control will be handed to the first catch block on the call stack. The program will end if no catch block exists among caller functions.

JavaScript throw statement

The syntax of the throw statement is:

throw expression;

Here, the expression specifies the value of the exception.

For example,

const number = 5;
throw number/0; // generate an exception when divided by 0

Note: The expression can be a string, boolean, number, or object value.

JavaScript throw with try-catch

The syntax of try…catch…throw is:

try {
    // body of try
    throw exception;
} 
catch(error) {
    // body of catch  
}

Note: When the throw statement is executed, the block is exited and the catch block is entered. Furthermore, the code following the throw command is not performed.

Example 1: try…catch…throw Example

const number = 40;
try {
    if(number > 50) {
        console.log('Success');
    }
    else {

        // user-defined throw statement
        throw new Error('The number is low');
    }

    // if throw executes, the below code does not execute
    console.log('hello');
}
catch(error) {
    console.log('An error caught'); 
    console.log('Error message: ' + error);  
}

Output

An error caught
Error message: Error: The number is low

  • In the above program, a condition is checked. If the number is less than 51, an error is thrown. And that error is thrown using the throw statement.
  • The string is specified in the toss statement. As an expression, the number is low.
  • For standard errors, you can also use the following built-in error constructors: TypeError, SyntaxError, ReferenceError, EvalError, InternalError, and RangeError.

For example:

throw new ReferenceError('this is reference error');

Rethrow an Exception

To rethrow an exception, use the throw statement inside the catch block. As an example,To rethrow an exception, use the throw statement inside the catch block. As an example:

const number = 5;
try {
     // user-defined throw statement
     throw new Error('This is the throw');

}
catch(error) {
    console.log('An error caught');
    if( number + 8 > 10) {

        // statements to handle exceptions
         console.log('Error message: ' + error); 
        console.log('Error resolved');
    }
    else {
        // cannot handle the exception
        // rethrow the exception
        throw new Error('The value is low');
    }
}

Output

An error caught
Error message: Error: This is the throw
Error resolved

  • To catch an exception, the throw statement is used within the try block in the preceding program. In addition, the throw statement is rethrown in the catch block, which is executed if the catch block is unable to handle the exception.
  • The catch block handles the exception in this case, and no error happens. As a result, the throw statement is not rethrown.
  • If the catch block did not handle the error, the throw statement would be rethrown with the error message.

Uncaught Error: The value is insufficient

You may like:

Switch Statement in JavaScript

Leave a Reply