Try? distributes, not returns zero

My understanding try?was that when evaluating the expression following try?, it nilwill be returned if there is an exception, otherwise the return value of the function is 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 related 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 can anyone help me understand why I try?did not catch the exception in this case?

+4
source share
1 answer

try? nil, Swift . a Objective-C NSException, , :

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00007fffbe5cb45d libobjc.A.dylib`objc_exception_throw
    frame #1: 0x00007fffa9855c3d CoreFoundation`+[NSException raise:format:] + 205
    frame #2: 0x00007fffab3a001a Foundation`+[NSJSONSerialization JSONObjectWithStream:options:error:] + 178

Objective-C Swift ( Objective-C, ., , " NSException Swift" ).

+4

Source: https://habr.com/ru/post/1676709/


All Articles