In this tutorial, you learn about JavaScript throw statements with the assistance of examples.
In this article, you will learn-
Definition and Usage
The throw statement throws (produces) an error.
At the point when a blunder happens, JavaScript will normally stop, and create a mistake message.
The technical term for this is: JavaScript will throw an error.
The throw proclamation allows you to create a custom error.
The technical term for this is: throw an exception.
The exception can be a JavaScript String, a Number, a Boolean, or an Object:
In the previous tutorial, you learn to handle exceptions using JavaScript try..catch statement. The try and catch proclamations handle exceptions in a standard manner which is given by JavaScript. Nonetheless, you can use the throw statement to pass user-defined exceptions.
In JavaScript, the throw statement handles user-defined exceptions. For instance, if a specific number is divided by 0, and in the event that you need to think about Infinity as an exception, you can use the throw statement to deal with that exception.
JavaScript throw statement
The syntax of throw statement is:
throw expression;
Here, 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 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, it exits out of the block and goes to the catch block. What’s more, the code beneath the throw statement isn’t executed.
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. In the event that the number is less than 51, a mistake is thrown. Furthermore, that mistake is thrown using the throw statement.
The throw statement specifies the string The number is low as an expression.
Note: You can likewise use other built-in-error error constructors for standard blunders: TypeError, SyntaxError, ReferenceError, EvalError, InternalError, and RangeError.
For instance,
throw new ReferenceError('this is reference error');
Rethrow an Exception
You can likewise use a throw statement inside the catch block to rethrow an exception. For instance,
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
In the above program, the throw statement is used inside the try block to catch an exception. What’s more, the throw proclamation is rethrown in the catch block which gets executed if the catch block can’t deal with the exception.
Here, the catch block handles the exception and no error happens. Subsequently, the throw articulation isn’t rethrown.
In the event that the error was not taken care of by the catch block, the throw articulation would be rethrown with the error message Uncaught Error: The worth is low
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.