Throw exception out of a function in JavaScript

Description

The following code shows how to throw exception out of a function.

Example


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function getMonthName (monthNumber) {<!-- w  ww.  j a  v a2 s .  co m-->
throw "InvalidMonthNumber"
}
try {
alert(getMonthName(13))
}
catch (exception) {
alert("An " + exception + " exception was encountered.  Please contact the program vendor.")

}

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

Click to view the demo

The code above generates the following result.

Throw exception out of a function 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