You can use the try... catch... finally statement to handle exceptions in Javascript, or manually throw exceptions.
1. Use the try... catch... finally statement to handle exceptions.
JSCodeIf an exception occurs during execution, an exception class object is manually created and submitted to the browser. This process is called "throwing an exception ". When the browser receives an object, it looks for code that can handle this exception and submits the current exception object to it for processing. This process is called "capturing exceptions ". The basic syntax format of try... catch... finally statements is:
Try {// code that may throw an exception
} Catch (error) {// code executed if an exception occurs. error is an exception class object.
} Finally {// unconditionally executed code
}
In the preceding statement, the catch statement is followed by the try statement, and the finally statement is followed by the catch statement. This is a complete statement for exception handling. In fact, both the catch statement and the finally statement can be omitted, but at least one of them must be used in combination with the try statement.
The statement in the try block does not have to throw an exception. Any JavaScript statement can be processed using an exception handling statement, but this is not necessary. If an exception is thrown in a line of code in the try block, the code below the line will not be executed, and the Catch Block Code will be executed directly.
In the Catch Block, the error in the brackets following the catch statement indicates the caught exception object instance. This instance contains detailed information about the exception and can be properly handled based on the information. If there is a finally statement after the catch statement, continue to execute the statement in the Finally block.
The statements in the Finally block are always executed. The statements in the block are usually cleaned up. If a transfer statement, such as return statement, continue statement, or break statement, is encountered before the Finally block is executed, the code in the Finally block will also be executed before the statements are executed.
If an exception handling statement contains only try .. if a catch statement does not have a replenishment exception, the finally BLOCK statement is executed directly after the statement in the try block is executed, and then the exception is thrown.
Example:
<SCRIPT>
Try {
VaR date = new date ();
Date. Test (); // call date's undefined test method;
Document. wrire ("Try block execution ended <br> ");
} Catch (error ){
With (document ){
Write ("an exception occurred <br> ");
Write ("exception type:" + error. Name + "<br> ");
Write ("exception message:" + error. Message );
}
} Finally {
Document. Write ("exception handling is complete! ");
}
</SCRIPT>
Result:
An exception occurred.
Exception type: typeerror
Exception message: the object does not support this attribute or the method has completed exception handling!
2. Manually throw an exception
In addition to the exception thrown by the browser during running, developers can also throw the exception by themselves. The throw statement is thrown when a manual exception occurs. Its basic syntax format is:
Throw expression;