IOS captures exceptions and common exception handling methods ., Ios capture

Source: Internet
Author: User
Tags throw exception

IOS captures exceptions and common exception handling methods ., Ios capture

During APP development, we usually need to capture exceptions to prevent sudden crashes of applications and to avoid unfriendly aspects to users. In fact, the exception handling method of OBJECT-C is the same as that of JAVA, a friend who understands JAVA can understand it at a glance. Why should I write this blog? I found that many of the methods described on Baidu are not what I want, but I cannot make it clear. The point is that everyone directly copies others' code... So let's not talk about it. Let's look at it ~~~

 

The following procedures have been tested and passed:

Device: In IOS8 Simulator

Development Tool: XCode6.1

 

Catch exceptions using @ try and catch:

The following is the simplest code syntax, where @ finally can be removed:

@ Try {// code that may crash} @ catch (NSException * exception) {// exception caught} @ finally {// result processing}

 

Here is a more detailed method to throw an exception:

@ Try {// 1 [self tryTwo];} @ catch (NSException * exception) {// 2 NSLog (@ "% s \ n % @", _ FUNCTION __, exception); // @ throw exception; // no exception can be thrown here} @ finally {// 3 NSLog (@ "I will definitely execute ");} // 4 // NSLog (@ "try") will be executed here ");

 

TryTwo method code:

-(Void) tryTwo {@ try {// 5 NSString * str = @ "abc"; [str substringFromIndex: 111]; // The program will crash here} @ catch (NSException * exception) {// 6 // @ throw exception; // throw an exception, that is, the last-level processing // 7 NSLog (@ "% s \ n % @", _ FUNCTION __, exception );} @ finally {// 8 NSLog (@ "tryTwo-I will definitely execute");} // 9 // if an exception is thrown, then this code will not execute NSLog (@ "if an exception is thrown here, this code will not execute ");}

To facilitate your understanding, I will explain the following situation:

If 6 throws an exception, the execution sequence is: 1-> 5-> 6-> 8-> 3-> 4. If 6 does not throw an exception, the execution sequence is: 1-> 5-> 7-> 8-> 9-> 3-> 4

 

2) We can't avoid the crash in some cases. Even QQ will crash. Therefore, we can perform some "actions" (collect error messages) before the program crashes. The following example sends the caught exceptions to the developer's mailbox.

AppDelegate. m

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. callback (& UncaughtExceptionHandler); return YES;} void UncaughtExceptionHandler (NSException * exception) {/*** get exception crash information */NSArray * callStack = [exception callStackSymbols]; NSString * reason = [exception rea Son]; NSString * name = [exception name]; NSString * content = [NSString stringWithFormat: @ "========= abnormal Error Report =========\ nname: % @ \ nreason: \ n % @ \ ncallStackSymbols: \ n % @ ", name, reason, [callStack componentsJoinedByString: @" \ n "]; /*** send the exception crash message to the developer email */NSMutableString * mailUrl = [NSMutableString]; [mailUrl appendString: @ "mailto: test@qq.com"]; [mailUrl appendString: @"? Subject = program exception crash. Please send the exception report together. Thank you for your cooperation! "]; [MailUrl appendFormat: @" & body = % @ ", content]; // open the NSString * mailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString: mailPath];}

 

For more information about email sending, see my other blog:

IOS development-e-mail (E-mail) method collection (3 in total)

 

Blog Author: GarveyCalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide the original article link. Thank you for your cooperation!

Related Article

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.