Catching errors in javaScript

Definition -: when browsing web pages on the internet, we all have seen a java script alert box telling us there is a  run time error and asking “Do you wish to debug ?”. error message like this may be useful for developers but not for users.

There are two types Catching errors in javaScript-: In the java script provide try… and catch statement in java script.

 

 try …. catch statement (available in IE5 ,Mozila,1,0 and netscpe 6) -: This statement allows us to test a block of code errors. The try block contains the code to be run and the catch block contains the code to be execute if an errors occurs.

<html>
<head>
<title>tips and Technic of computer and internet</title>

<script language=”javascript” type=”text/javascript”>
var txt=””
function message()
{
try
{
addalert(“welcome netnic”)
}
catch(err)
{
txt=”there is a error on page\n\n”
txt+=”click ok to continuous viewing this page\n”
txt+=”or cancel to return to he home page\n\n”
if(!confirmed(txt))
{
document.locatin.href=”http://www.netnic.org”
}
}
}
</script>
</head>
<body>
<input type=”button” value=”view message” onclick=”message”/>
</body>
</html>

Throw Statement-: The throw statement allows us to create an exception. If we use this statement together with the try….catch statement we can control program flow and generate accurate error message. hear are example have been given

<html>
<head>
<title>tips and technic of computer and internet</title>
</head>
<body>

<script language=”javascript” type=”text/javascript”>
var x=prompt(“enter the number betweeb 0 to 10;”,””)

{
try
{
if(x>10)
throw”err1″
else if(x<0)
throw”err2″
}
catch(er)
{
if(er==”err1)
alert(“Error! The value is to high”)
if(er==”err2)
alert(“Error! the value is too low
}
}
</script>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *