An introduction to exception capture in JavaScript _javascript tips

Source: Internet
Author: User

Like the Java language, JavaScript can throw exceptions by throw statements. Unlike the Java language, JavaScript can throw all types of values through throw statements, not just the wrong objects.


Copy Code code as follows:

Throw an Error object.
try{
throw new error ("Message in Error Object");
}catch (e) {
Console.log (e);//error:message in Error Object
}

try{
Throw "Raw message";
}catch (message) {
Console.log (message);//raw message
Console.log (typeof message);//string
}

try{
Throw 42;
}catch (code) {
Console.log (code);//42
Console.log (typeof code);//number
}


As with the Java language, if an exception is not captured by any catch statement, the exception is eventually thrown at the user:


Copy Code code as follows:

try{
throw new error ("Test Error");//error'll be thrown. Error:test Error
}finally{
}

try{
Throw 42;//error would be thrown. Error:42
}finally{
}


For a catch that throws an exception, JavaScript uses the try/catch/finally statement, which uses the rule that: try is required, catch and finally are optional statements, but at least one of the catch and finally must appear.

In a catch statement, you can define an argument e (or any other valid variable name) to store the thrown exception value. Within a catch statement, this parameter can be used as a local variable. Unlike other variables used in JavaScript, a parameter variable in a catch statement is only valid inside a catch statement (the scope of the variable is limited to a catch statement).

For a finally statement, the code in finally is executed regardless of whether an exception is thrown in the try. Details include:

No exception occurs in 1.try, and the code in finnally is executed when the try statement completes.
No exception occurred in 2.try, but the finally code will be executed because the break, continue, or return statement was executed, resulting in the exit of the try code.
An exception occurred in 3.try and the code in finally was executed after the exception was processed by the catch statement.
An exception occurred in 4.try, but the code in finally will be executed because there is no catch statement that causes the exception to continue to be thrown upwards. It is worth noting that, in the absence of a catch statement, JavaScript executes the code in finally before continuing to throw an exception up.

In finally code, if a break, continue, or return statement occurs, JavaScript executes the statements directly, while the possible break, continue, or return statements in the try code are ignored Even if a catch statement is missing and an exception needs to be escalated, JS discards the exception escalation and continues execution of the break, continue, or return statement in the finally code. Similarly, if an exception is thrown in the finally code, then JavaScript discards the break, continue, or return statement in all the try codes and discards the possible exception escalation behavior, throwing only the exception in the finally code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.