IOS Capture Program exception

Source: Internet
Author: User

In the development of IOS , we will encounter the case that the program throws an abnormal exit, and if it is in the process of debugging, the information of the exception is clear at a glance, but it is sometimes difficult to get the information of the exception in the published program.

IOS provides an exception to the processing API, we can add such a Handlerwhen the program starts, such a program when an exception can be done in this part of the information to do the necessary processing, Timely feedback to the developer.

First define a class Signalhandler

@interface signalhandler: nsobject

+ (instancetype) Instance;

-(void) Setexceptionhandler;

@end

Implement SIGNALHANDLER.M

#import "SignalHandler.h"

#import <UIKit/UIKit.h>

#include <libkern/OSAtomic.h>

#include <execinfo.h>

@interface signalhandler ()

{

BOOL isdismissed;

}

-(void) HandleException1: (nsexception *) exception;

@end

callback function after capturing the signal

void handleexception (nsexception *exception)

{

// handling exception messages

[[signalhandler Instance] performselectoronmainthread:@selector( HandleException1:) withobject: Exception waituntildone:YES];

}

@implementation Signalhandler

Static signalhandler *s_signalhandler = nil;

+ (instancetype) instance{

static dispatch_once_t oncetoken;

dispatch_once(&oncetoken, ^{

if (s_signalhandler = = Nil) {

S_signalhandler = [[signalhandler alloc] init];

}

});

return s_signalhandler;

}

-(void) Setexceptionhandler

{

Nssetuncaughtexceptionhandler(&handleexception);

}

methods used to handle exceptions

-(void) HandleException1: (nsexception *) exception

{

cfrunloopref runloop = cfrunloopgetcurrent();

cfarrayref allmodes = cfrunloopcopyallmodes(runloop);

// exception stack information

nsarray *stackarray = [exception callstacksymbols];

// Causes of anomalies

nsstring *reason = [exception reason];

// exception name

nsstring *name = [exception name];

nsstring *exceptioninfo = [nsstring stringwithformat:@ " cause:%@\n Name:%@\ N Stacks:%@ ", name, Reason, Stackarray];

NSLog(@ "%@", exceptioninfo);

    // Here you can also save file

uialertview *alertview = [[uialertview alloc] initwithtitle:@ "There's a problem with the program. " message: Exceptioninfo delegate:self cancelbuttontitle:@" Cancel " otherbuttontitles:nil";

[Alertview show];

// when receiving an exception handling message, let the program start runloopto prevent the program from dying

while (! Isdismissed) {

for (nsstring *mode in (__bridge nsarray *) allmodes)

{

Cfrunloopruninmode((cfstringref) mode, 0.001, false);

}

}

// when clicking the Cancel button of the popup view Oh , isdimissed = Yes, the top loop jumps out

cfrelease(allmodes);

Nssetuncaughtexceptionhandler(NULL);

}

-(void) Alertview: (uialertview *) Analertview clickedbuttonatindex: (nsinteger) anindex

{

// because this pop-up view has only one Cancel button, the isdimsmissed variable is modified directly.

isdismissed = YES;

}

Finally, you need to

Add in Didfinishlaunchingwithoptions

[[signalhandler Instance] setexceptionhandler];

Once you're done, you can verify it and add the following code

nsarray *array = [nsarray arraywithobjects:@ "1", Nil];

[Array objectatindex:2];

IOS Capture Program exception

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.