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!