How should I use NSSetUncaughtExceptionHandler in Swift How should I use NSSetUncaughtExceptionHandler in Swift swift swift

How should I use NSSetUncaughtExceptionHandler in Swift


As of Swift 2 (Xcode 7), you can pass Swift functions/closuresto parameters taking a C function pointer. From the Xcode 7 release notes:

Native support for C function pointers: C functions that take function pointer arguments can be called using closures or global functions, with the restriction that the closure must not capture any of its local context.

So this compiles and works:

func exceptionHandler(exception : NSException) {    print(exception)    print(exception.callStackSymbols)}NSSetUncaughtExceptionHandler(exceptionHandler)

Or with an "inline" closure:

NSSetUncaughtExceptionHandler { exception in    print(exception)    print(exception.callStackSymbols)}

This does exactly the same as the corresponding Objective-C code:it catches otherwise uncaught NSExceptions.So this will be caught:

let array = NSArray()let elem = array.objectAtIndex(99)

NOTE:- It does not catch any Swift 2 errors (from throw) or Swift runtime errors, so this is not caught:

let arr = [1, 2, 3]let elem = arr[4]


Update

With Swift 2, you can pass Swift functions and closures as C function pointer. See Martin R's answer below.

Original answer

You can't, as of Xcode 6 beta 6.

Swift does support passing around function pointers, but they're treated pretty much like opaque pointers. You can't neither define a C function pointer to a Swift function nor can you call a C function pointer in Swift.

That means you call NSSetUncaughtExceptionHandler() from Swift, but the handler must be implemented in Objective-C. You need a header file like this:

volatile void exceptionHandler(NSException *exception);extern NSUncaughtExceptionHandler *exceptionHandlerPtr;

and in the implementation, you need something like this:

volatile void exceptionHandler(NSException *exception) {    // Do stuff}NSUncaughtExceptionHandler *exceptionHandlerPtr = &exceptionHandler;

After you imported the header file in the Swift bridging header, you can set up the exception handler as usual:

NSSetUncaughtExceptionHandler(exceptionHandlerPtr)


The error you can see at the reopen of a subsequent application in this way.

This code for swift 4.Add in didFinishLaunchingWithOptions() :

NSSetUncaughtExceptionHandler { exception in            print("Error Handling: ", exception)            print("Error Handling callStackSymbols: ", exception.callStackSymbols)            UserDefaults.standard.set(exception.callStackSymbols, forKey: "ExceptionHandler")            UserDefaults.standard.synchronize()        }

And The code add in fistViewController viewLoad()

// ERROR ExceptionHandler        if let exception = UserDefaults.standard.object(forKey: "ExceptionHandler") as? [String] {            print("Error was occured on previous session! \n", exception, "\n\n-------------------------")            var exceptions = ""            for e in exception {                exceptions = exceptions + e + "\n"            }            AlertFunctions.messageType.showYesNoAlert("Error was occured on previous session!", bodyMessage: exceptions, {            }, no: {                UserDefaults.standard.removeObject(forKey: "ExceptionHandler")                UserDefaults.standard.synchronize()            })        }

EDIT:The code work. But you need reopen your application after error.