You are on page 1of 1

Error Handling

Try... Catch

<html>
<head>
<script type="text/javascript">
var txt=" ";
function message()
{
try
{ alert("welcome"); }
catch(err)
{
txt="there was an error"\n;
txt+="error description:"+ err.description+"\n;
txt+="click OK to continue\n\n";
alert(txt);
}
}
</script>
</head>
<body>
<input type="button" value="view message" onclick="message()"/>
</body>
</html>

Throw Statement
<html>
<body>
<script type="text/javascript">
var x=prompt(" enter a num between 0 & 10:"," ");
try{
if(x>10)
{ throw "err1";}
else if
{throw "err2";}
}
catch(er)
if(er=="err1")
{ alert("error! too high");}
if(er=="err2")
{alert("error! too low");}
}
}
</script>
</body>
</html>

You might also like