gpt4 book ai didi

macos - NSSetUncaughtExceptionHandler 在 Mac 上不起作用

转载 作者:行者123 更新时间:2023-12-03 16:20:16 24 4
gpt4 key购买 nike

我的代码在这里:

myAppDelegate.m中:

void catchException(NSException *e)
{
NSLog( @"here is a exception");
}

@implementation myAppDelegate
{
NSSetUncaughtExceptionHandler(&catchException);
NSException *e = [[NSException alloc] initWithName: @"aException" reason: @"test" userInfo: nil];
@throw e;
}

但是函数catchException永远不会被捕获。谁能告诉我为什么?

最佳答案

我在自己的 OSX 代码中发现,我必须使用 NSExceptionHandler 才能可靠地捕获异常:

#import <Cocoa/Cocoa.h>
#import <ExceptionHandling/NSExceptionHandler.h>
#import "CocoaUtil.h"
#import <execinfo.h>

// ExceptionDelegate
@interface ExceptionDelegate : NSObject
@end
static ExceptionDelegate * _exceptionDelegate = nil;

int main(int argc, const char **argv) {
int retval = 1;

@autoreleasepool
{
//
// Set exception handler delegate
//
_exceptionDelegate = [[ExceptionDelegate alloc] init];
NSExceptionHandler *exceptionHandler = [NSExceptionHandler defaultExceptionHandler];
exceptionHandler.exceptionHandlingMask = NSLogAndHandleEveryExceptionMask;
exceptionHandler.delegate = _exceptionDelegate;

//
// Set signal handling
//
int signals[] = {
SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGEMT, SIGFPE, SIGBUS, SIGSEGV,
SIGSYS, SIGPIPE, SIGALRM, SIGXCPU, SIGXFSZ
};
const unsigned numSignals = sizeof(signals) / sizeof(signals[0]);
struct sigaction sa;
sa.sa_sigaction = signalHandler;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
for (unsigned i = 0; i < numSignals; i++)
sigaction(signals[i], &sa, NULL);


// Do work

} // @autoreleasepool

return retval;
}

static void dumpStack(void **frames, unsigned numFrames) {
char **frameStrings = backtrace_symbols(frames, numFrames);
if (frameStrings) {
for (unsigned i = 0; i < numFrames && frameStrings[i] ; i++)
logbareutf8((char *)frameStrings[i]);
free(frameStrings);
} else {
logerr(@"No frames to dump");
}
}

static void signalHandler(int sig, siginfo_t *info, void *context) {
logerr(@"Caught signal %d", sig);

const size_t maxFrames = 128;
void *frames[maxFrames];
unsigned numFrames = backtrace(frames, maxFrames);
dumpStack(frames, numFrames);

bool send = criticalAlertPanel(@"Application Error",
@"Upload logfile to support site",
@"MyApp is terminating due to signal %d", sig);
if (send)
sendLogfile();

exit(102);
}

static void sendLogfile() {
// Redacted
}

@implementation ExceptionDelegate

- (BOOL)exceptionHandler:(NSExceptionHandler *)exceptionHandler
shouldLogException:(NSException *)exception
mask:(NSUInteger)mask {

logerr(@"An unhandled %@ exception occurred: %@", [exception name], [exception reason]);

// [exception callStackReturnAddresses] will be empty, so parse the NSStackTraceKey from
// [exception userInfo].
NSString *stackTrace = [[exception userInfo] objectForKey:NSStackTraceKey];
NSScanner *scanner = [NSScanner scannerWithString:stackTrace];
NSMutableArray *addresses = [[NSMutableArray alloc] initWithCapacity:0];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *token;
while ([scanner scanUpToCharactersFromSet:whitespace
intoString:&token]) {
[addresses addObject:token];
}

NSUInteger numFrames = [addresses count];
if (numFrames > 0) {
void **frames = (void **)malloc(sizeof(void *) * numFrames);
NSUInteger i, parsedFrames;

for (i = 0, parsedFrames = 0; i < numFrames; i++) {
NSString *address = [addresses objectAtIndex:i];

if (![CocoaUtil parseString:address toVoidPointer:&frames[parsedFrames]]) {
logerr(@"Failed to parse frame address '%@'", address);
break;
}

parsedFrames++;
}

if (parsedFrames > 0) {
logerr(@"Stack trace:");
dumpStack(frames, (unsigned)parsedFrames);
}

free(frames);
} else {
logerr(@"No addresses in stacktrace");
}

return YES;
}

- (BOOL)exceptionHandler:(NSExceptionHandler *)exceptionHandler
shouldHandleException:(NSException *)exception
mask:(NSUInteger)mask {

bool send = criticalAlertPanel(@"Application Error",
@"Upload logfile to support site",
@"MyApp is terminating due to an unhandled exception:\n\n%@",
[exception reason]);
if (send)
sendLogfile();

exit(101);

// not reached
return NO;
}

关于macos - NSSetUncaughtExceptionHandler 在 Mac 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23336296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com