Try, catch, and finally execution rules in js

Source: Internet
Author: User
This article mainly uses instance parsing to better understand the execution rules of try, catch, and finally in js. It has good reference value. Let's take a look at try: the error of the statement test code block, put the code that may cause errors here.

Catch: the code is executed only when an error occurs in the code block in try. The err parameter records the error message of the Code in try.

Finally: the code will be executed no matter whether there is any exception

Try {console. log (0);} catch (err) {console. log (1); console. log (hello);} finally {console. log (2);} // the final result is 0. 2/* try {. b. c ();} catch (e) {console. log (1); console. log (hello);} finally {console. log (2);} * // the final result is printed. 1 2 error: hello is not defined/* try {. b. c ();} catch (e) {console. log (1); try {console. log (hello);} catch (e) {console. log (3) ;}} finally {console. log (2); console. log (word);} * // the final result is printed 1 3 2 error: word is not defined/* try {. b. c ();} catch (e) {console. log (1); console. log (hello);} finally {console. log (2); console. log (word);} * // the final result is printed. 1 2 error: word is not defined.

Summary:

When an error occurs in the code in try, the code in catch will be executed, and the code in finally will always be executed.

In catch and finally, normal code will be executed from top to bottom

If there is an error in the code in catch, the error in catch is reported.

If both catch and finally errors occur, the finally errors will be reported.

The above is all the content of this article. I hope this article will help you in your study or work, and I also hope to support PHP!

For more articles about try, catch, and finally execution rules in js, please follow the PHP Chinese website!

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.