Catching NSJSONSerialization errors in Swift
<p>I'm trying to understand why I'm unable to catch the errors thrown by NSJSONSerialization. </p>

<p>I expect the <code>NSInvalidArgumentException</code> exception to be raised and caught, but instead the app crashes.</p>

<p>This is occurring in both Swift 3 and Swift 2.3 using Xcode 8.</p>

<p><strong>Swift 3:</strong></p>

<pre><code>    do {
        _ = try JSONSerialization.data(withJSONObject: ["bad input" : NSDate()])
    }
    catch {
        print("this does not print")
    }
</code></pre>

<p><strong>Swift 2.3:</strong></p>

<pre><code>    do {
        _ = try NSJSONSerialization.dataWithJSONObject(["bad input" : NSDate()], options: NSJSONWritingOptions())
    }
    catch {
        print("this does not print")
    }
</code></pre>

<p>This code is put in <code>applicationDidFinishLaunching</code> inside a blank Xcode project. Tested on both simulator and device.</p>

<p>Full exception:</p>

<pre><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSDate)'
</code></pre>

<p>Any ideas why the catch block is not catching this particular error?</p>