0

I'm trying to catch all NSExceptions in a Swift3 program - have been following this Stack Overflow answer

So I've got this:

override func viewDidLoad() {
    super.viewDidLoad()
    NSSetUncaughtExceptionHandler { exception in
        print("EXCEPTION CAUGHT HERE....")
        print(exception)
        print(exception.callStackSymbols)
    }

    // force NSException
    let array = NSArray()
    _ = array.object(at: 99)

}

but the exception's not being called Instead I get this output:

2016-09-04 14:49:14.252 catchAllErrorsTest[8126:164827] Failed to set (contentViewController) user defined inspected property on (NSWindow): *** -[__NSArray0 objectAtIndex:]: index 99 beyond bounds for empty NSArray

I also tried putting the exception into AppDelegate (in applicationWillFinishLaunching and applicationDidFinishLaunching), but same result

Community
  • 1
  • 1
Jon Cook
  • 127
  • 1
  • 11
  • The link you shared clearly states that it does not catch any Swift 2 errors `(from throw)` or Swift runtime errors such as accessing 99th index of array `[1, 2, 3]`. – Ozgur Vatansever Sep 04 '16 at 05:02
  • thanks @ozgur - I agree that the link states it won't catch Swift errors, but it seems to clearly state the code quoted will work (it says 'So this will be caught:') - and it certainly looks like me like that should throw an NSException (though I'm no expert) – Jon Cook Sep 04 '16 at 09:42

1 Answers1

-1

I did as follows and works for me in Swift 3. Define your NSSetUncaughtExceptionHandler as a constant outside any method declaration in your AppDelegate:

let uncaughtExceptionHandler : Void = NSSetUncaughtExceptionHandler { exception in
NSLog("Name:" + exception.name.rawValue)
if exception.reason == nil
    {
        NSLog("Reason: nil")
    }
    else
    {
        NSLog("Reason:" + exception.reason!)
    }

}

This fires at any uncaught exception as this constant is evaluated as soon as your App Delegate instance loads i.e. at app launch. Note: the Void type declaration removes a compiler warning for a Void inferred type that "could be unexpected" ;-) .

stefat
  • 154
  • 1
  • 4
  • 1
    However it's not working for me- I've created a new Swift 3 project - I put your code in AppDelegate.swift - outside of any methods (directly below "import cocoa". Then in – Jon Cook Oct 16 '16 at 09:20
  • ... argh... sorry ... Then in viewDidLoad, after super.viewDidLoad, I put the same code to force the error ( // force NSException let array = NSArray() _ = array.object(at: 99)) – Jon Cook Oct 16 '16 at 09:21
  • and I got "2016-10-16 20:16:45.345 testUncaughtException[3408:163505] Failed to set (contentViewController) user defined inspected property on (NSWindow): *** -[__NSArray0 objectAtIndex:]: index 99 beyond bounds for empty NSArray" – Jon Cook Oct 16 '16 at 09:22
  • but the uncaughtExceptionHandler was not called – Jon Cook Oct 16 '16 at 09:22
  • @JonCook Did you find any solution for this problem? I'm also facing the same behavior . – Sandeep Kumar Jun 29 '18 at 22:45
  • @SandeepKumar - sorry it's been a long time since I was in that code base.. I can't recall.. I just checked the code and i ended up commenting out that whole handler,but I don't recall what I replaced it with... will let you know if I recall – Jon Cook Jul 01 '18 at 04:28