JSONSerialization.data(withJSONObject:options:)
(aka dataWithJSONObject
in Swift 2) is declared throws
. However, passing invalid objects causes a crash, not a catchable error:
do {
// Crash
try JSONSerialization.data(
withJSONObject: NSObject(),
options: [])
}
catch
{
// Never reached
print("Caught error:", error)
}
Why is that method declared “throws,” then? Under what circumstances does it throw an exception?
Not knowing what causes an error to be thrown makes it hard to know how to handle the error, and makes it impossible to write tests that verify that handling.