I'm trying to understand why I'm unable to catch the errors thrown by NSJSONSerialization.
I expect the NSInvalidArgumentException
exception to be raised and caught, but instead the app crashes.
This is occurring in both Swift 3 and Swift 2.3 using Xcode 8.
Swift 3:
do {
_ = try JSONSerialization.data(withJSONObject: ["bad input" : NSDate()])
}
catch {
print("this does not print")
}
Swift 2.3:
do {
_ = try NSJSONSerialization.dataWithJSONObject(["bad input" : NSDate()], options: NSJSONWritingOptions())
}
catch {
print("this does not print")
}
This code is put in applicationDidFinishLaunching
inside a blank Xcode project. Tested on both simulator and device.
Full exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSDate)'
Any ideas why the catch block is not catching this particular error?