Two simple instances allow you to quickly understand Try-catch usage

Source: Internet
Author: User

Tag: ess cli inner html. com: HTTP doctype code

 

I believe that in actual projects, you may often see code similar to the following:

Try {// try to execute the code block} catch (ERR) {// capture the wrong code block} finally {// how the result will be executed code block}

 

In short, the above code is used to handle possible error messages in the code.

 Try {}Indicates the code to be executed.

  Catch {}For captureTry {}Error message in Code Execution

  Finally {}Code that will be executed anyway

I will list an example below. Sometimes the text description is not intuitive and easy to understand. I will try to understand the relevant code and try to understand it. Maybe it will be easy to understand.

<! Doctype HTML> 

 

  Note:The preceding example shows that the show () method in the clickme () method is executed after clicking the button. Because this method is not defined, an error is returned, prompting that this method is not defined.

  

 

The following example describesCatch {}The error message is captured throughThrowHow to create a custom message

<! Doctype HTML> 

 

 Supplement:

①: Try and catch (must be used together)

②: Try and catch and throw (if you want to create a custom message, use it together)

③: Try, catch, and finally (finally is not mandatory and optional)

  

Two simple instances allow you to quickly understand Try-catch usage

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.