3

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.

Community
  • 1
  • 1
Durai Amuthan.H
  • 28,889
  • 6
  • 148
  • 223
  • How did you test that? (You cannot catch Swift runtime errors.) – Martin R Feb 23 '16 at 15:05
  • Oh ! Is there anyway to catch swift runtime errors ? I have updated the question with the snippet that I use to crash the app. – Durai Amuthan.H Feb 24 '16 at 05:50
  • As far as I know...swift is running on the same runtime as Objective C – Durai Amuthan.H Feb 24 '16 at 05:53
  • I tried Crittercism it caught this error.But I don't want to implement any third party I want to catch my own errors and keep it inside the app. – Durai Amuthan.H Feb 24 '16 at 06:16
  • I don't know what Crittercism is or does. To the best of my knowledge, you *cannot* catch Swift runtime errors (like `Array` out of bounds access, or unwrapping an `Optional` which is `nil`). – Martin R Feb 24 '16 at 06:18

1 Answers1

0

Initialize Crittercism before installing the NSSetUncaughtExceptionHandler.

And at the beginning of your handler, call:

[Crittercism logHandledException:exception]; // ObjC

or Crittercism.logHandledException(exception) // Swift

vmanjz
  • 1,046
  • 1
  • 9
  • 21
  • I am trying to catch the crash just using `NSSetUncaughtExceptionHandler` or some other native code without using third party like *Crittercism* – Durai Amuthan.H Jul 08 '16 at 06:38