Tuesday, June 26, 2012

Java Script Tutorial 17 : JavaScript Try .. Catch

JavaScript try .. catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs. Syntax of JS try .. catch is :


try
  {
       // code
  }
catch()
  {
       // Handle errors
  }


For example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var txt="";
function Showmessage()
{
     try
       {
            adddlert("JS try .. catch testing ....");
       }
     catch(err)
       {
            txt="There was an error on this page.";
            alert(txt);
       }
}
</script>
</head>
<body>
   <input type="button" value="Click to View message" onclick="Showmessage()" />
</body>
</html>

No comments:

Post a Comment