JavaScript: Understanding Try...catch...finally

Source: Internet
Author: User
Tags finally block try catch

Before, I always like to use Console.log (do some thing) to perform the output type and value, want to see the popup immediately, it will be directly in the browser alert (), these are the basics.

A little more complex, it is necessary to use the judgment statement, if else to determine the condition, say if condition else otherwise, such a judgment for the code Lennon is already very familiar with writing the program.

If you think this is also very simple, you may use the mixed if else conditional judgment statement plus a try catch to process the statement, although with a try catch can handle any object, throw a wrong statement by throw, and then catch throws the object or the object error, Today we only say Try...catch, the following examples throw arrays, time, prototype functions, numeric types, and so on.

Array = [234= = 12.22 array + ' \ n ' + newdate.tolocalestring () + ' \ n ' + fun.prototype.constructor + ' \ n ' + (is = = ' number ') + ' \ n ' + call; ' Err finally '

More accurately, try to put a statement that could produce an error. When a try statement starts executing and throws an error, the catch executes the inner statement and the corresponding try error message in the message. When the finally statement is executed, the finally statement is executed only after the try statement and the catch statement are executed, regardless of whether the try throws an exception or the catch capture executes the finally statement.

function Trycatch () {try{throw new Error (' Koringz ');    } catch (E) {console.log (e.message);    } finally{Console.log (' err finally '); }}trycatch ()//output://koringz//Err finally

Throwing a wrong statement through a try, we see that the catch captures an incorrect message//Koringz, but the same finally also outputs the/ERR finally. While we understand how the try catch workflow is handled, we do not understand the code handlers of the finally block, as we have always thought of the finally statement, which is that the finally output is not constrained by try and catch. Here are a few output demo codes for finally:

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, the try throws a wrong statement, and the result of the finally output 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, the try throws a wrong statement, and the catch catches the error output to the end. Err finally//new finally.

When I modify a try statement:

function Trycatch () {try{//} catch (E) {console.log (' err finally '); Return Console.log (' new finally ')}}trycatch ()//null (VIOD)//Empty (Viod)

As a result, the output is empty. Empty (Viod). Because the try does not throw an error, the catch does not catch an exception, so the output is empty.

So let's take a look at the following example, which might give you a better understanding of the exception handling of a try catch statement.

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 we nest a try catch in a block of code that may throw an error in a try, throw a statement that may be wrong in a nested code block try to throw a new error (' Open '), and immediately after the nested try passes the error to the nested catch processing, Finally, after the nested finally run, we see the last Result//Op open, in fact the error message of the nested catch catch is thrown to the outermost catch capture. OP Open

That is, any given exception will only be captured once by the enclosing catch block closest to it.

Of course, any new exceptions thrown in the "inner" block (because the code in the catch block can also throw exceptions) will be captured by the “ external ” block.

Reference: Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch

Reference: Https://msdn.microsoft.com/library/4yahc5d8 (v=vs.94). aspx

This article is from the "This Is Share blog" blog, please be sure to keep this source http://kosig.blog.51cto.com/9218084/1702319

JavaScript: Understanding Try...catch...finally

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.