JavaScript Throw Statement Example

JavaScript throw statement allows you to create and throw user defined exception

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Throw Statement in JavaScript</title>
</head>
<body>
<script>
    var x = 10, y = 5;

    try {
        if(x == y)
            throw "e1";
        else
            throw "e2";
    } catch(e) {
        if(e == "e1")
            alert("Exception: Both are same value!");
        if(e == "e2")
            alert("Exception: Both are different!");
    }
</script>
</body>
</html>

Run it...   »