How do we use throw statement in JavaScript?

In this tutorial, we will learn to use the throw statement in JavaScript. The "throw" is a reserved keyword in JavaScript that allows programmers to create user-defined exceptions.

Every programmer makes errors, and sometimes users enter invalid input that causes exceptions. While JavaScript has built-in exceptions like arithmetic and index out-of-bound exceptions, programmers often need to create custom exceptions using the throw statement inside try...catch blocks.

Below, we have given different examples of using the throw keyword in JavaScript.

Using the throw keyword to create exception

In this section, we will learn to create simple user-defined exceptions using the throw statement. When we throw an error, program execution control goes to the nearest catch block to handle the exception. If no catch block is defined, program execution terminates due to the exception.

Syntax

try {
    // program code
    if (condition) {
        throw 'error message';
    }
} catch (error) {
    // handle user-defined exception
}

Example

In the below example, we are throwing an error when the input number plus 10 becomes greater than 25. We are throwing a simple error message.

<html>
<head>
    <title>Example - Use of the throw keyword in JavaScript</title>
</head>
<body>
    <h2>Use of the throw keyword in JavaScript</h2>
    <h4>Program execution flow, when we use the throw keyword to throw a user defined exception</h4>
    <div id="value"></div>
    <script>
        let value = document.getElementById("value");
        try {
            value.innerHTML += "control is inside the try block. <br>";
            let number = 20;
            if (number + 10 > 25) {
                // throw error
                throw "number is large, it is not acceptable.";
                value.innerHTML += "inside if block. <br>";
            }
        } catch (error) {
            // handling the exception
            value.innerHTML += "inside the catch block. <br>";
            value.innerHTML += "error message is: " + error + " <br>";
        }
    </script>
</body>
</html>
control is inside the try block. 
inside the catch block. 
error message is: number is large, it is not acceptable.

In the above output, you can see that execution of the try block is not completed due to throwing an error. The control goes to the catch block and handles the error.

Using the throw Statement to Create a New Error Object

In the previous section, we created a simple error message to throw the exception. In this section, we will create a new user-defined error object. JavaScript has a built-in Error class to manage exceptions. We can create an instance of the Error class and pass the error message as an argument.

Syntax

try {
    // program code
    if (condition) {
        throw new Error("message");
    }
} catch (error) {
    // handle user-defined exception
}

Example

In the below example, we are checking the type of a variable. If the variable type is undefined, we throw a user-defined error object with an error message and handle the exception in the catch block.

<html>
<head>
    <title>Example - Use of the throw keyword in JavaScript</title>
</head>
<body>
    <h2>Use of the throw statement in JavaScript</h2>
    <h4>Program execution flow, when we use the throw keyword to throw a user defined exception</h4>
    <div id="value"></div>
    <script>
        let value = document.getElementById("value");
        try {
            value.innerHTML += "control is inside the try block. <br>";
            if (typeof number == "undefined") {
                // throw error
                throw new Error("variable is undefined.");
            }
            value.innerHTML += "terminating the try block. <br>";
        } catch (error) {
            value.innerHTML += "inside the catch block. <br>";
            value.innerHTML += error.name + ": " + error.message + " <br>";
        }
    </script>
</body>
</html>
control is inside the try block. 
inside the catch block. 
Error: variable is undefined.

Rethrowing the exception using throw Statement

In this section, rather than handling the error in the catch block, we will throw the error again from the catch block.

Syntax

try {
    if (condition) {
        throw new Error("message");
    }
} catch (error) {
    throw "unknown error.";
}

Example

In the example below, we are throwing two different errors based on conditional statements. We handle one error in the catch block and rethrow the second error from the catch block.

<html>
<head>
    <title>Use of the throw keyword in JavaScript</title>
</head>
<body>
    <h2>Rethrowing the exception using throw Statement</h2>
    <h4>Program execution flow, when we use the throw keyword to throw a user defined exception</h4>
    <div id="value"></div>
    <script>
        let value = document.getElementById("value");
        try {
            value.innerHTML += "control is inside the try block. <br>";
            let a = 20;
            let b = 30;
            if (a + b > 30) {
                throw "large number";
            } else if (a + b < 30) {
                throw "small number";
            }
            value.innerHTML += "terminating the try block. <br>";
        } catch (error) {
            // handling the exception
            value.innerHTML += "inside the catch block. <br>";
            if (error == "large number") {
                value.innerHTML += "handling the large number. <br>";
            } else {
                throw "small number exception.";
            }
        }
    </script>
</body>
</html>
control is inside the try block. 
inside the catch block. 
handling the large number.

Conclusion

The throw statement allows you to create custom exceptions in JavaScript. You can throw simple strings, Error objects, or rethrow exceptions for more complex error handling scenarios.

Updated on: 2026-03-15T23:18:59+05:30

790 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements