加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iOS版.当其他代码已经使用NSSetUncaughtExceptionHandler时,我怎

发布时间:2020-12-14 17:19:12 所属栏目:百科 来源:网络整理
导读:我想使用全局异常处理程序. 调用applicationdidFinishLaunchingWithOptions: NSSetUncaughtExceptionHandler(uncaughtExceptionHandler); 并使用它来处理异常: void uncaughtExceptionHandler(NSException *exception) { // handling exception} 但我也使
我想使用全局异常处理程序.

调用applicationdidFinishLaunchingWithOptions:

NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

并使用它来处理异常:

void uncaughtExceptionHandler(NSException *exception) {
        // handling exception
}

但我也使用sdk,它已经使用了NSSetUncaughtExceptionHandler.然后我的方法uncaughtExceptionHandler在异常发生时没有调用.

我知道一个app只能是一个处理程序.但我需要它和sdk,这个代码可以处理全局级别的异常.

在这种情况下,您有什么想法我可以使用NSSetUncaughtExceptionHandler吗?或者其他想法如何在全球范围内处理异常?
十分感谢.

解决方法

在SDK NSSetUncaughtExceptionHandler将覆盖处理程序之后调用installUncaughtExceptionHandler().将调用你的uncaughtExceptionHandler.

此外,您可以存储SDK的处理程序,并在uncaughtExceptionHandler中调用它以使SDK协同工作.

示例代码:

static NSUncaughtExceptionHandler *exceptionHandler = NULL;

typedef void (*sighandler_t)(int);
static sighandler_t sigHandler = NULL;

static void handleException(NSException *e)
{
    //your code ...

    //call the SDK handler
    if (exceptionHandler) {
        exceptionHandler(e);
    }
}

static void handleSignal(int signal)
{
    //your code ...

    if (sigHandler) {
        sigHandler(signal);
    }
}

void installUncaughtExceptionHandler()
{
    // store the SDK handler
    exceptionHandler = NSGetUncaughtExceptionHandler();

    NSSetUncaughtExceptionHandler(&handleException);

    sigHandler = signal(SIGABRT,handleSignal);
    signal(SIGILL,handleSignal);
    signal(SIGSEGV,handleSignal);
    signal(SIGFPE,handleSignal);
    signal(SIGBUS,handleSignal);
    signal(SIGPIPE,handleSignal);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读