Try — Catch In JavaScript

 
It is very difficult to analyze all the situations to arise during runtime of a program because of which some conditions may remain unanswered, these unanswered conditions are called exceptions, JavaScript provides a facility to handle such situations. We can use try and catch statements to handle such situations. The whole code is kept inside the try block an in case any exception arises it is thrown using throw statement which is caught by the catch statement.
 
Example:
 
<html>

<head>

<title>events </title>

</head>

<body bgcolor=blue text=”white” >

<h4> Enter Either Value as Zero (0)</h4>

Value 1: <input type=”text” id=”t1″> <br>

Value 2: <input type=”text” id=”t2″> <br>

Answer: <input type=”button” id=”b1″ value=”Hit me ” onclick=”pp()”> <br>

<script>

function pp()

{

var c= parseInt(document.all.t1.value)

var d=parseInt(document.all.t2.value)

var e=0

try

{

if(d==0)

{

throw “Divisor is Zero”

}

if (c==0)

{

throw “To be divided is Zero”

}

}

catch(error) {

alert(error)

}

}

</script>

</body>