NSSetUncaughtExceptionHandler not catches the exce

2019-05-14 01:36发布

问题:

Purpose of NSSetUncaughtExceptionHandler is to catch the exception and dump it somewhere so that we can find why the app crashed and what is the exception.

In Objective C it catches the exception as expected..Here is the reference

But in swift when an exception arises its not caught. Here is the code that I used in didFinishLaunchingWithOptions

 NSSetUncaughtExceptionHandler { exception in
            NSUserDefaults.standardUserDefaults().setObject("Exception Details Are \n\nExceptionName--> \(exception.name) \nReason -->\(exception.reason!)\n\(exception.description)", forKey: "Exception")
            NSUserDefaults.standardUserDefaults().setObject("Call Stack Symbols:\(exception.callStackSymbols)", forKey: "ExceptionCallstack")
            NSUserDefaults.standardUserDefaults().synchronize()
        }

Here is the snippet that I use to crash the app

    var c=["a","b","c"]
    var d=c[4]

Can anybody tell me what I am doing wrong here.

回答1:

Initialize Crittercism before installing the NSSetUncaughtExceptionHandler.

And at the beginning of your handler, call:

[Crittercism logHandledException:exception]; // ObjC

or Crittercism.logHandledException(exception) // Swift