iOS development essay--ios catching exceptions, common exception handling methods

Source: Internet
Author: User

When developing an app, we often need to catch exceptions, prevent the application from suddenly crashing, and prevent users from having an unfriendly experience. In fact, objective-c exception handling methods and Java similarities, understand the Java friends A look on the understanding. Why should I write this blog post? Because I found that the introduction of Baidu, a lot of it is not what I want, and I want to say it is not clear, the point is that we are directly copy other people's Code ... So not much to say, we look down ~ ~ ~ Original Address: http://www.cocoachina.com/ios/20141229/10787.html

The following programs have been tested and passed:

Device: in IOS 8 simulator

Development tools: XCode6.1

Catch exceptions using @try, catch:

Here's the simplest code notation, where @finally can get rid of:

@ try {      // 可能会出现崩溃的代码 } @ catch (NSException *exception) {      // 捕获到的异常exception } @finally {      // 结果处理 }

Here's a more detailed way to throw an exception:

@ try {      // 1      [self tryTwo]; } @ catch (NSException *exception) {      // 2      NSLog(@ "%s\n%@" , __FUNCTION__, exception); //        @throw exception; // 这里不能再抛异常 } @finally {      // 3      NSLog(@ "我一定会执行" ); } // 4 // 这里一定会执行 NSLog(@ "try" ); - (void)tryTwo {      @ try {          // 5          NSString *str = @ "abc" ;          [str substringFromIndex:111];  // 程序到这里会崩      }      @ catch (NSException *exception) {          // 6 //        @throw exception; // 抛出异常,即由上一级处理          // 7          NSLog(@ "%s\n%@" , __FUNCTION__, exception);      }      @finally {          // 8          NSLog(@ "tryTwo - 我一定会执行" );      }           // 9      // 如果抛出异常,那么这段代码则不会执行      NSLog(@ "如果这里抛出异常,那么这段代码则不会执行" ); }

For your convenience, let me explain the situation here:
If 6 throws an exception, then the order of execution is: 1->5->6->8->3->4
If 6 does not throw an exception, then the order of execution is: 1->5->7->8->9->3->4

2) Part of the collapse of the situation we are unavoidable, even QQ will have a crash. So we can do some "action" (Collect the error message) before the program crashes, the following example is to send the caught exception to the developer's mailbox.

Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      // Override point for customization after application launch.      NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);      return YES; } void UncaughtExceptionHandler(NSException *exception) {      /**       *  获取异常崩溃信息       */      NSArray *callStack = [exception callStackSymbols];      NSString *reason = [exception reason];      NSString *name = [exception name];      NSString *content = [NSString stringWithFormat:@ "========异常错误报告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@" ,name,reason,[callStack componentsJoinedByString:@ "\n" ]];      /**       *  把异常崩溃信息发送至开发者邮件       */      NSMutableString *mailUrl = [NSMutableString string];      [mailUrl appendString:@ "mailto:[email protected]" ];      [mailUrl appendString:@ "?subject=程序异常崩溃,请配合发送异常报告,谢谢合作!" ];      [mailUrl appendFormat:@ "&body=%@" , content];      // 打开地址      NSString *mailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailPath]];

iOS development essay--ios catching exceptions, common exception handling methods

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.