Strategy 1-11: Handling Exceptions

Source: Internet
Author: User

1. Add a button action

-(Ibaction) Throwfakeexception: (UIButton *) Sender {    * e = [[NSException alloc] Initwithname:@ " fakeexception " Reason:@" Thedeveloper Sucks! " userinfo:[nsdictionary dictionarywithobject:@"Extra info" forkey:@"  Key"]];    [E raise];}

2. Register a handler function with the Nssetuncaughtexception function, which is a void function that accepts a nsexception reference as a unique parameter.

////APPDELEGATE.M//errorhanding////Created by Feng Min on 16/4/29.//copyright©2016 year fengmin. All rights reserved.//#import "AppDelegate.h"#import<MessageUI/MessageUI.h>#import<MessageUI/MFMailComposeViewController.h>@interfaceAppdelegate () <uialertviewdelegate, mfmailcomposeviewcontrollerdelegate>@end@implementationappdelegatevoidExceptionhandler (NSException *exception) {NSLog (@"uncaught exception:%@\nreason:%@\nuser info:%@\ncall stack:%@", Exception.name,exception.reason,exception.userinfo,exception.callstacksymbols); //Set FlagNsuserdefaults * settings =[Nsuserdefaults Standarduserdefaults]; [Settings Setbool:yes Forkey:@"Exceptionoccurredonlastrun"]; [Settings synchronize];}-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions {
Predefined conditional macros that use our exception handling code only when the application is running on a real device. #if! Target_iphone_simulator//Default excepion handling codeNsuserdefaults * settings =[Nsuserdefaults Standarduserdefaults]; if([Settings Boolforkey:@"Exceptionoccurredonlastrun"]) { //Reset exception occurred flag[Settings Setbool:no Forkey:@"Exceptionoccurredonlastrunkey"]; [Settings synchronize]; //Notify the userUialertview * Altert = [[Uialertview alloc] Initwithtitle:@"We are sorry!"Message@"An error occurred on the previous run!" Delegate: Nil Cancelbuttontitle:@"Dismiss"Otherbuttontitles:nil]; [Altert addbuttonwithtitle:@"Email a report"]; [Altert show]; }Else{ //Register exception handle functionNssetuncaughtexceptionhandler (&Exceptionhandler); //Redirect stderr output stream to fileNsarray * paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString* Documentspath = [Paths Objectatindex:0]; NSString* Stderrpath = [Documentspath stringbyappendingpathcomponent:@"Stderr.log"]; Freopen ([Stderrpath cstringusingencoding:nsasciistringencoding],"W", stderr); }#endif returnYES;}- (void) Alertview: (Uialertview *) Alertview Diddismisswithbuttonindex: (nsinteger) buttonindex{Nsarray* Paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString* Documentspath = [Paths Objectatindex:0]; NSString* Stderrpath = [Documentspath stringbyappendingpathcomponent:@"Stderr.log"]; if(Buttonindex = =1) { //Email a reportMfmailcomposeviewcontroller * Mailcomposer =[[Mfmailcomposeviewcontroller alloc] init]; Mailcomposer.mailcomposedelegate=Self ; [Mailcomposer setsubject:@"Error Report"]; [Mailcomposer settorecipients:[nsarray Arraywithobject:@"[email protected]"]]; //Attach log FileNsarray * paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString* Documentspath = [Paths Objectatindex:0]; NSString* Stderrpaht = [Documentspath stringbyappendingpathcomponent:@"Stderr.log"]; NSData* data =[NSData Datawithcontentsoffile:stderrpath]; [Mailcomposer addattachmentdata:data MimeType:@"Text/xml"FileName:@"Stderr.log"]; Uidevice* device =[Uidevice Currentdevice]; NSString* Emailbody = [NSString stringWithFormat:@"My model:%@\nmy os:%@\nmy version:%@", device.model,device.systemname,device.systemversion]; [Mailcomposer setmessagebody:emailbody Ishtml:no]; [Self.window.rootViewController presentviewcontroller:mailcomposer Animated:yes Completion:^{ }]; } nssetuncaughtexceptionhandler (&Exceptionhandler); //Redirect stderr output stream to fileFreopen ([Stderrpath cstringusingencoding:nsasciistringencoding],"W", stderr);}- (void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: (mfmailcomposeresult) result Error: (Nserror *) error{[self.window.rootViewController dismissviewcontrolleranimated:yes Completion:^{ }];}

Strategy 1-11: Handling Exceptions

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.