Jan 17, 2010

How to catch an error in JavaScript

This example code explains us clearly how to catch an error in
javascript. Onclick of button we just calling a javascript function
"message()". What this "message()" is trying to do here is nothing. It
has to pop an alert with "Welcome guest!".
But see the code clearly, we have syntax error in javascript try
block. instead of "alert" we wrote "adddlert". Actually "adddlert" is
not a keyword in javascript. So this html page will get an error at
runtime.
Here we catching the error description using " err.description ".
Simple. Just try once!!!

File : tryCatch_JS.html

<code>
<html>
<head>
<script type="text/javascript">
var txt="";
function message()
{
try
{
adddlert("Welcome guest!");
}
catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.description + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body></html>
</code>
--
Publicize Yourself... Free!
http://saalram.com

No comments:

Post a Comment

Saalram.com

Saalram
Publicize yourself ...
http://saalram.com

Free !!!


Place your Ad here - Free!!!
just drop us a mail ... saalram.service@gmail.com

Free !!!