Understanding try... catch... finally in javascript,

Source: Internet
Author: User
Tags php try catch

Understanding try... catch... finally in javascript,

This article analyzes how to use try... catch... finally in javascript and shares it with you for your reference. The specific content is as follows:

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.

Function trycatch () {var array = [234], newdate = new Date (), fun = function () {}, is = 12.22, call; try {throw array + '\ n' + newdate. toLocaleString () + '\ n' + fun. prototype. constructor + '\ n' + (typeof is = 'number') +' \ n' + call; // be careful that there is an 'e'} catch (e) after the local) {console. log (e);} finally {console. log ('err finally ');} trycatch () // output: // 234 // 10:07:03 // function () {}// true // undefined

More accurately, put a statement in a try that may generate an error. 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

If you try to throw an incorrect statement, we can see that the catch caught an error message // koringz, 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. 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)

The output is 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{  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 try catch is nested in a code block that may cause an Error in try, throw new Error ('open') is thrown through the nested code block try ');, next, the nested try will pass the error to the nested catch process. After the nested finally run, we will see the last result // op open, in fact, the nested catch error information is thrown to the outermost catch. // Op open

That is to say, 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 exceptions) will be captured by the "external" block.

The above is all the content of this article, and I hope it will help you learn javascript programming.

Articles you may be interested in:
  • How to use JavaScript Try... Catch Declaration
  • Php Try Catch exception Test
  • Try catch finally execution sequence for in-depth analysis
  • PowerShell: Try... Catch... Finally Implementation Method
  • Detailed description of try catch exceptions in php
  • Try catch finally Usage Analysis in C #

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.