Macos NSSetUncaughtExceptionHandler不';我不能在mac上工作
我的代码在这里: 在Macos NSSetUncaughtExceptionHandler不';我不能在mac上工作,macos,cocoa,Macos,Cocoa,我的代码在这里: 在myAppDelegate.m中: void catchException(NSException *e) { NSLog( @"here is a exception"); } @implementation myAppDelegate { NSSetUncaughtExceptionHandler(&catchException); NSException *e = [[NSException alloc] initWithName: @
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;
}
#导入
#进口
#导入“CocoaUtil.h”
#进口
//例外公使
@接口例外Legate:NSObject
@结束
静态ExceptionLegate*_ExceptionLegate=nil;
int main(int argc,常量字符**argv){
int-retval=1;
@自动释放池
{
//
//设置异常处理程序委托
//
_exceptionDelegate=[[exceptionDelegate alloc]init];
NSExceptionHandler*exceptionHandler=[NSExceptionHandler defaultExceptionHandler];
exceptionHandler.exceptionHandlingMask=NSLogAndHandleEveryExceptionMask;
exceptionHandler.delegate=\u exceptionDelegate;
//
//设置信号处理
//
整数信号[]={
SIGQUIT、SIGILL、SIGTRAP、SIGABRT、SIGEMT、SIGFPE、SIGBUS、SIGSEGV、,
SIGSYS、SIGPIPE、SIGALRM、SIGXCPU、SIGXFSZ
};
const unsigned numSignals=sizeof(信号)/sizeof(信号[0]);
struct-sigaction-sa;
sa.sa_sigaction=信号处理器;
sa.sa_flags=sa_SIGINFO;
sigemptyset(和sa.sa_面具);
for(无符号i=0;i0){
void**frames=(void**)malloc(sizeof(void*)*numFrames);
nsui整数,解析的框架;
for(i=0,parsedFrames=0;i0){
logerr(@“堆栈跟踪:”);
转储堆栈(帧,(无符号)解析的帧);
}
自由(帧);
}否则{
logerr(@“stacktrace中没有地址”);
}
返回YES;
}
-(BOOL)exceptionHandler:(NSExceptionHandler*)exceptionHandler
shouldHandleException:(NSException*)异常
掩码:(整数)掩码{
bool send=criticalAlertPanel(@“应用程序错误”,
@“将日志文件上载到支持站点”,
@“由于未处理的异常,MyApp正在终止:\n\n%@”,
[例外原因];
如果(发送)
sendLogfile();
出口(101);
//未达到
返回否;
}
您需要导入特洛伊木马注意到的ExceptionHandling.framework
以下是清理堆栈跟踪打印的要点(某些功能缺失):
该代码无法编译。请提供将要编译的代码。我忘记了这一行:-(void)applicationdFinishLaunching:(NSNotification*)实现后的提示和结束