Introduction to exception capturing in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces exception capture in JavaScript. This article explains how to throw an exception by throwing a throw statement and capturing an exception by trycatchfinally statement. For more information, see the same Java language, javaScript can throw an exception through the throw statement. Different from the Java language, JavaScript can throw all types of values through throw statements, instead of throwing error objects.


The code is 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
}


Like Java, if an exception is not caught by any catch statement, the exception is finally thrown to the user:


The code is as follows:


Try {
// Throw new Error ("test error"); // Error will be thrown. Error: test error
} Finally {
}

Try {
Throw 42; // Error will be thrown. Error: 42
} Finally {
}


For capturing thrown exceptions, JavaScript uses try/catch/finally statements. The rules are as follows: try is required, and catch and finally are optional statements, but at least one catch and finally must appear.

In a catch statement, you can define a parameter e (or any other valid variable name) to store the thrown outlier. In a catch statement, this parameter can be used as a local variable. Different from other variables in JavaScript, the parameter variables in the catch statement are only valid within the catch statement (the scope of the variable is limited to the catch statement ).

For finally statements, the code in finally statements will be executed no matter whether exceptions are thrown in try. Details include:

1. there is no exception in try. after the try statement is executed, the code in finnally will be executed.
2. there is no exception in try, but the code in finally will be executed when the try code is exited due to the execution of the break, continue, or return statement.
3. an exception occurs in try. after The exception is handled by the catch statement, the finally code is executed.
4. if an exception occurs in try, but the exception is not caused by a catch statement, the finally code will be executed when the exception needs to be thrown up. It is worth noting that when the catch statement is missing, JavaScript will first execute the finally code before continuing to throw an exception.

In finally code, if the break, continue, or return statements appear, JavaScript will directly execute these statements, and ignore the possible break, continue, or return statements in try code; even if the catch statement is missing and an exception needs to be reported, JS discards the exception reporting information and continues to execute the break, continue, or return statement in finally code. Similarly, if an exception is thrown in finally code, JavaScript will discard the break, continue, or return statements in all try code, and also discard possible exception reporting behaviors, only exceptions in finally code are thrown.

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.