Catch and row exceptions in JavaScript

Description

The following code shows how to catch and row exceptions.

Example


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function myErrorHandler(data){<!--from  www.ja  v  a2 s  .  c o m-->
try{
try{
if(data == "string"){
throw "E0";
}else{
throw "E1";
}
}catch(e){
if(e == "E0"){
return("Error (" + e + "): Entry must be numeric.");
}else{
throw e;
}
}
}catch (e){
return("Error (" + e + "): Entry was invalid.");
}
}
function processData(form){
if(isNaN(parseInt(form.myText.value))){
alert(myErrorHandler("string"));
}else{
alert("You have correctly entered a number");
}
}
</script>
</head>
<body>
<form name="myForm">
Please enter a number:
<input type=TEXT size=10 value="" name="myText">
<input type=BUTTON value="Process" name="myButton" onClick='processData(this.form)'>
</form>
</body>
</html>

Click to view the demo

The code above generates the following result.

Catch and row exceptions 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