My understanding of try?
was that when the expression following try?
is evaluated, nil
will be returned if there is an exception, otherwise the return value of the function will be returned.
When I run the following code:
guard let istream = InputStream(url: url),
let ambiguousObj = try? JSONSerialization.jsonObject(with: istream, options: []),
let jsonObj = ambiguousObj as? [[String: Any]] else {
throw ExportError.recoveredParseFailed
}
I get an error pertaining to the second line above:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization JSONObjectWithStream:options:error:]: stream is not open for reading'
I understand how to fix the error. (I needed to call istream.open()
)
But, could someone help me understand why try?
didn't catch the exception in this case?