How to catch a Swift crash and do some logging

后端 未结 4 1951
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 13:48

In Objective-C, the NSSetUncaughtExceptionHandler can register some functionality to do some last minute logging about the exception.

This doesn\'t catc

4条回答
  •  没有蜡笔的小新
    2020-12-16 14:29

    NSSetUncaughtExceptionHandler works only with NSExceptions. See this SO answer for brilliant explanation.

    To catch Swift run time errors implement signal handler for SIGTRAP. As far as I know, Swift code will terminate the program with SIGTRAP exception type if it detects an unexpected condition at runtime i.e only SIGTRAP is helpful to catch Swift errors, rest like SIGSEGV, SIGBUS, SIGILL do not work. I found this info in this apple link.

    If your code is a mix both Objective-C and Swift, then implement both NSSetUncaughtExceptionHandler and signal handler to handle crashes.

    For understanding and implementing Signal handling refer this link.

    Hope this helps.

提交回复
热议问题