Try catch user exception in JavaScript

Description

The following code shows how to try catch user exception.

Example


<html>
<head>
<script type="text/javascript">
<!--   w w w  .  jav  a  2 s.  com-->
function addTwoNumbers(a, b) {
throw new Error("Two numbers are required.");
}

try {
result = addTwoNumbers(90);
} catch (oException) {
alert(oException.message);
}

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Try catch user 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