Error Handling
Error in Javascript

Error in Javascript

Error has basically two type

1- Compile time error
This generate an error at the compile time means before running code.

2- Run time error
When code successfully then javascript generate runtime error.
runtime error like suppose we use funcion  and calling function not define in your code then this generate runtime error
so for removing runtime error we use try/catch block

try...catch Statement

By try catch statement we can handle runtime error.

Syntax for try/catch block

try
  {

  //code here

  }
catch(err)
  {

  //error handling code here

  }

Example to use of try/catch block

<html>
<head>
<title>Error handling</title>
<script language="javascript" type="text/javascript">
function show()
{
try
  {
  add(7,8);
  }
catch(e)
  {
  alert("Runtime error\n"+e.description);
 }
}
</script>
</head>
<body>
<input type="button" value="show value" onclick="show(4,6)" />
</body>
</html>


Previous
Next