Try catch exception capture mechanism usage analysis _javascript techniques in JavaScript

Source: Internet
Author: User
Tags throw exception try catch

The examples in this article describe the use of Try catch exception capture mechanisms in JavaScript. Share to everyone for your reference, specific as follows:

1. Like Java, JavaScript also has a try catch block, which is the mechanism for exception capture.

(1) A typical try Catch statement

try{
}
catch{
}
finally{
}

As in Java, the most typical try catch statement in JS is also divided into three parts, and try is used to catch exceptions, catch to handle exceptions, and finally to close resources for subsequent operations.

Example:

try{
  Throw "error"
}
catch (ex)
{
  console.log (ex);
}
finally{
  Console.log ("finally")
}

Console output in turn: error,finally

(2) in try,catch blocks, catch blocks and finally blocks only need one, so the following try catch blocks are also possible

Example:

try {
  throw "error"
}
finally{
}

Throw exception only, other case is not an example

(3) A try catch block is included in a try Catch block

try{ 
   try{
       Throw "error"
     }
   finally{
      console.log ("Finally1")
     }
\
catch (ex)
{
   Console.log (ex)
}
finally{
  console.log ("Finally2")
}

In the case of nested loops like this, the order of output is: finally1,error,finally2

(4) Nested try catch blocks, throwing exceptions

try{
   try{
      throw "Error1"
   }
   catch (ex)
   { 
      console.log (ex);
      Throw "Error2"
   }
   finally{
     console.log ("Finally1")
   }
catch (ex)
{
    Console.log (ex);
}
finally{
   console.log ("Finally2")
}

The final output is: error1,finally1,error2,finally2

More readers interested in JavaScript-related content can view the site topics: "JavaScript error and debugging skills Summary", "JavaScript value-handling Skills Summary", "JavaScript Coding Operation Skills Summary", " JavaScript in the JSON Operation tips Summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and Skills summary", "JavaScript data structure and algorithm skills summary , JavaScript traversal algorithms and techniques summary and JavaScript mathematical Operational Usage Summary

I hope this article will help you with JavaScript programming.

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.