How to get Class , method and line number of uncaught exception using NSSetUncaughtExceptionHandler

牧云@^-^@ 提交于 2019-12-07 18:46:33

问题


I used NSSetUncaughtExceptionHandler(&HandleExceptions); for handling uncaught exceptions in the method

void HandleExceptions(NSException *exception)
{
}   

I want to print Class , method and line number where that exception occurred any solution??

I am getting Following stack trace

0   appName                            0x00000001000b6a20 HandleExceptions + 584
    1   appName                            0x00000001002175f0 GAIUncaughtExceptionHandler + 716
    2   CoreFoundation                      0x00000001852fe95c <redacted> + 692
    3   libobjc.A.dylib                     0x0000000195a103b4 <redacted> + 116
    4   libc++abi.dylib                     0x0000000195211bb4 <redacted> + 16
    5   libc++abi.dylib                     0x000000019521173c __cxa_rethrow + 144
    6   libobjc.A.dylib                     0x0000000195a10294 objc_exception_rethrow + 44
    7   CoreFoundation                      0x00000001851e1154 CFRunLoopRunSpecific + 572
    8   GraphicsServices                    0x000000018e38b5a4 GSEventRunModal + 168
    9   UIKit                               0x0000000189b163c0 UIApplicationMain + 1488
    10  appName                            0x00000001000a8584 main + 108
    11  libdyld.dylib                       0x000000019607ea08 <redacted> + 4

回答1:


The handler gets an instance of NSException. There are two methods: -callStackSymbols and -callStackReturnAddresses.

-callStackSymbolsgives you the backtrace strings, if it is possible.

It looks like this:

0   TestArrayCopy                       0x0000000100000dfe -[MyClass method] + 46
1   TestArrayCopy                       0x0000000100000e97 main + 87
2   libdyld.dylib                       0x00007fff940be5fd start + 1
3   ???                                 0x0000000000000001 0x0 + 1

As you can see, there is an information about the method owner (- for instance methods), the class name (MyClass) and the method name (method).



来源:https://stackoverflow.com/questions/27960178/how-to-get-class-method-and-line-number-of-uncaught-exception-using-nssetuncau

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!