Exception trapping mechanism

Source: Internet
Author: User

When writing code, the most afraid of code to write hundreds of thousands of lines, but a running program crashes, in order to improve the robustness of the Code, the following provides a way to improve the robustness of the code:

  Exception trapping mechanism

1 // exception capture mechanism: Improve code robustness 2 @try---@catch---@finally  

The following is a small demo of a printed array to illustrate its usage

1Nsarray *arr = @[@Ten, @ -, @ -];2     @try {3         /*put code that is likely to cause a problem that causes the program to crash in the try statement body*/4NSLog (@"arr[3] =%@", arr[3]);5     }6     @catch(NSException *exception) {7         /*If an exception occurs, the program does not crash, jumps to the catch statement body, and can add some hints when an exception occurs in the body of the catch statement*/8NSLog (@"exception:%@", exception);9     }Ten     @finally { One         /*the contents of the finally statement body will be executed regardless of the presence of a wood.*/ ANSLog (@"finally: The anomaly mechanism is over!"); -}

In the code above, the response is annotated with code that shows how the exception capture mechanism works and how to improve the robustness of the code.

Here is the result of the operation:

Exception trapping mechanism

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.