Example 1:
This example shows how to catch an exception and displays information about the error:
try {
unknownFunction ();
}
catch(e) {
document.write ("The error message: <b>" + e.message + "</b>");
if ('number' in e) {
document.write ("<br />");
document.write ("The error code: <b>" + e.number + "</b>");
}
if ('lineNumber' in e) {
document.write ("<br />");
document.write ("The error occurred at line: <b>" + e.lineNumber + "</b>");
}
}
Example 2:
This example shows how to create and catch a custom exception:
try {
var err = new Error ();
err.message = "My first error message";
if (err.fileName === undefined) { // IE, Opera, Google Chrome and Safari
err.fileName = document.location.href;
}
throw err;
}
catch(e) {
document.write ("The error message: <b>" + e.message + "</b><br />");
document.write ("The error occurred in the following file: <b>" + e.fileName + "</b>");