try catch finally is the ECMAScript-262 third edition standard that provides exception handling mechanism. The grammar structure is as follows:
try{ //可能会发生的错误代码 } catch(error){ //错误处理 }finally{ //无论是否有异常都会执行 }
Grammar and Like most languages such as java.net, if the try{} code block catches an exception, the catch block will get an error message object (an instance of Error).
We should put the code that may cause errors in the try block, and error handling in the catch block; in js, if an error occurs in the code block and there is no If the exception is caught, the subsequent code of the current code blocks such as:
On the contrary, the subsequent code will still be executed, such as:
If there is a finally code block, then no matter what The code in the reason will be executed, even if there is a return statement in the catch statement, the following code:
function say() { try { console.log(age) return; } catch (erroe) { console.log(erroe.message);//age is not defined return; } finally { console.log('finally 执行了');//finally 执行了 } } say();
Understand the Error type
When an error occurs when the code is running, an Error object will be created and its Throw, this object contains error description information.
For example, in the try...catch(error){...} statement, Error is an object thrown by the Error type. The object has three basic attributes: name, error name, message Error information, stack error stack information;
There are many types of errors that may occur during the period of execution code, so Error dispatched several sons such as:
# E error error. See, if there are any, they are thrown by the browser; the main purpose of this base type is for developers to throw custom errors.
Evalerror Create an Error instance to indicate the reason for the error: it is related to Eval ().
InternalError to create an example of an internal error that represents the internal error of the JavaScript engine. Such as: "Too much recursion".
Rangeerror created an ERROR instance to indicate the reason for the error: numerical variables or parameters exceeded its effective range.
ReferenceError Create an error instance to indicate the cause of the error: invalid reference.
Syntaxerror Create an ERROR instance to indicate the reason why the error is: evac () the syntax error occurred during the parsing code.
Typerror Create an ERROR instance, indicating the cause of the error: variables or parameters are not effective types.
URIError Create an error instance to indicate the cause of the error: the parameters passed to encodeURI() or decodeURl() are invalid.
Error is the base class. Other error types are inherited from the Error type, so the subclasses also have three basic attributes: name, error name, message, and stack.
With these error types, we can write code like this to specifically handle a certain type of exception by judging the type of exception, such as:
##
The above is the detailed content of js exception handling try catch finally explanation. For more information, please follow other related articles on the PHP Chinese website!