Catch an error in JavaScript
Description
The following code shows how to catch an error.
Example
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Throw that error!</TITLE>
<SCRIPT>
function throwError(errString) {<!--from ww w . j ava2s . c o m-->
try {
throw new Error (42, errString);
}
catch(e){
alert("Error number: " + e.number + "; Description: " + e.description)
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm">
Error text:
<INPUT type=text name=errText size=40>
<INPUT type=button name=btnThrow value="Throw it!"
onClick="throwError(document.theForm.errText.value);">
</FORM>
</BODY>
</HTML>
The code above generates the following result.
Javascript Tutorial Exception
Catch SyntaxError Exception 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
Catch SyntaxError Exception in JavaScript
Catch an error in JavaScript
Catch and row exceptions in JavaScriptCatch 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