Javascript: try... catch... finally

Source: Internet
Author: User
Tags finally block

Javascript: try... catch... finally
I used to like to use the console. log (do some thing) to execute the output type and value. If you want to immediately see the pop-up information, it will be directly in the browser alert (). These are the basic knowledge. If else is a little more complex, it is necessary to use a judgment statement. if else is used to determine the condition, then if condition else is not used. Such a judgment is very familiar with coding. If you think this is also very simple, you may use the mixed if else condition judgment statement and try catch to process the statement. Although try catch can be used to process any object, throw a wrong statement and catch the object to throw the error. Today we only talk about try... the following example throws an array, time, prototype function, and number type. Array = [234 = 12.22 array + '\ n' + newdate. toLocaleString () + '\ n' + fun. prototype. constructor + '\ n' + (is = 'number') +' \ n' + call; 'err finally 'more accurately, put a statement in try that may produce errors. When a try statement is executed and an error is thrown, the catch statement executes the internal statement and the error message in the corresponding try statement. When to execute the finally statement, the finally statement is executed only after the try statement and catch statement are executed. The finally statement is executed no matter whether the try throws an exception or catch. Function trycatch () {try {throw new Error ('koringz');} catch (e) {console. log (e. message);} finally {console. log ('err finally ');} trycatch () // output: // koringz // err finally throws an incorrect statement through try, we can see that an error message // koringz is captured in catch, but the same finally output // err finally. Although we understand how to process a try catch workflow, we do not know the finally block code processing program. In the past, we consistently thought about finally statements, the finally output is not restricted by try and catch. Below are several finally output demo codes: function trycatch () {try {throw new Error ('koringz');} finally {console. log ('err finally '); return console. log ('new finally ')} trycatch () // err finally // new finally as shown above. try to throw an incorrect statement and the finally output result is: // err finally // new finally. Function trycatch () {try {throw new Error ('koringz');} catch (e) {console. log ('err finally '); return console. log ('new finally ')} trycatch () // err finally // new finally as shown above. try to throw an incorrect statement and catch the error output result as same as finally. // Err finally // new finally. When I modify the try statement: function trycatch () {try {//} catch (e) {console. log ('err finally '); return console. log ('new finally ')} trycatch () // null (viod) results are empty. // Null (viod ). Because try does not throw an error, catch does not catch the exception, so the output result is null. Let's take a look at the following case. The following example may help you better understand the Exception Handling of try catch statements. Try {throw new Error ('open');} catch (e) {console.info (e. message); throw e} finally {console. log ('Finally ') ;}} catch (e) {console. log ('Op ', e. message);} // open // finally // op open when we nest try catch in a code block that may cause an error in try, throw a possible Error statement throw new Error ('open') in the nested code block try, and then the nested try will pass the Error to the nested catch, finally, after the nested finally operation, we can see the last result // op open. In fact, the nested catch error information is thrown to the outermost catch. // Op open: any given exception is captured only once by the closed catch Block closest to it. Of course, any new exceptions thrown in the "internal" block (because the code in the catch block can also throw an exception) will be captured by the & ldquo; external & rdquo; block.

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.