6. Customized exception information
The above six Error types basically cover the errors that may occur when the script program is running. In addition to these types, we can also use the Error constructor to customize exception types, with the syntax as follows:
myError = new Error(msg)
Where the msg parameter represents the message attribute value of the new exception defined. At the same time, we can also create a new object type as a subtype of the Error:
function MyError(msg) {
= "MyError"
= msg
}
= new Error;
Then we can create an instance of the custom error subclass:
myError = new MyError("My error message")
7. Trigger exceptions
After creating an Error object, you can use the throw statement to trigger the corresponding exception. The syntax of Throw is as follows:
throw errObj
errObj must be an Error object or a subtype of Error. After an exception is triggered in the try block code, the control will be transferred directly to the catch block.
In the following code, an exception is triggered in the try block, setting the exception information to "ooops", and then the control is transferred to the catch block:
var s
try {
s = "one "
throw new Error("oops")
s += "two"
} catch(err) {
s +=
}
s += " three"
alert(s)
The following prompt box appears after running:
There are many advantages to writing code to trigger exceptions, such as being beneficial to customizing error types, quickly transferring to catch block execution, and passing errors to the outer layer in nested exceptions as described below.
8. Nested exception handling
JavaScript supports multi-level nested exception handling. Generally, we can catch and handle errors in the catch code block handled by internal exceptions, and then trigger the exception again, so that we can further perform more in-depth processing in the catch code block handled by external exceptions. Let’s take a look at an example of nested exception handling:
var inner;
var outer;
try {
("Beginning outer try block, no exceptions yet");
try{
("Beginning inner try block, no exceptions yet");
// Generate a reference error
(undefinedVariable)
("Finished inner try block with no exceptions");
} catch(inner) {
// Internal exception handling
("Exception caught, beginning inner catch block");
("Error type: " + );
("Error message: " + );
throw inner;
("No exceptions thrown in inner catch block");
} finally {
("Executing inner finally block");
}
("Finished outer try block with no exceptions");
} catch(outer) {
// External exception handling
("Exception caught, beginning outer catch block");
("Error type: " + );
("Error message: " + );
} finally {
("Executing outer finally block");
}
The output results after execution are as follows:
Beginning outer try block, no exceptions yet
Beginning inner try block, no exceptions yet
Exception caught, beginning inner catch block
Error type: ReferenceError
Error message: undefinedVariable is not defined
Executing inner finally block
Exception caught, beginning outer catch block
Error type: ReferenceError
Error message: undefinedVariable is not defined
Executing outer finally block
The advantage of nested exception handling is that it allows us to handle errors in stages, internal exception handling can be responsible for solving script code problems caused by errors, and external exception handling is responsible for the feedback information provided to the user or logging the exception information.
9. Conclusion
This article discusses in detail a very important feature of the JavaScript language, "exception handling". Web developers should master it well and handle it flexibly in actual applications, so that HTML pages containing script code are truly indescribable and understanding.
Previous page12Read the full text