Catch SyntaxError Exception in JavaScript

Description

The following code shows how to catch SyntaxError Exception.

Example


<html>
<head>
<script type="text/javascript">
try {<!-- w  ww  . j  a  v  a  2 s.  co m-->
eval("a ++ b");        //causes SyntaxError
} catch (oException) {
if (oException instanceof SyntaxError) {
document.writeln("Syntax Error: " + oException.message);
} else {
document.writeln("An unexpected error occurred: " + oException.message);
}
}
</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Catch SyntaxError Exception in JavaScript
Home »
  Javascript Tutorial »
    Statement »
      Exception
Javascript Tutorial Exception
Catch SyntaxError Exception in JavaScript
Catch an error in JavaScript
Catch and row exceptions in JavaScript
Catch method name not found exception in Ja...
Check the exception name and output excepti...
Create an Error with error code and message...
Get Exception message in JavaScript
Get Exception name in JavaScript
Throw exception out of a function in JavaSc...
Try catch user exception in JavaScript