Is there any difference in how exceptions are handled under the hood between 'try?' and 'do/catch'?

I'm reviewing some errors, and I notice that some of them occur in JSONEncoder, where there's a 'try' and I have a 'do/catch' to catch the exception.
However, these errors don't reach the 'catch,' causing the app to crash.
My question is whether there's any difference between using a 'try?' that returns nil or using a 'catch'? Could it be that the error caught by 'catch' is different from simply returning nil? Or, beneath all this, is it exactly the same?
I'm not understanding why some errors aren't being handled, leading to the app's termination.
Thanks in advance. Regards.

I think the issue is that Swift can catch NSErrors thrown from Obj-C but JSONEncoder can also throw an NSException and that will crash your app even if you use try/catch when calling it from Swift.

There are workarounds, but none of them are great. I think a google search can give a better explanation than I can.

No difference in which errors can be propagated. If your app is crashing despite your code being in a docatch block then it is not due to a recoverable error in that code.

However, these errors don't reach the catch, causing the app to
crash.

The issue here is that the term exception is horribly overloaded. I explains this in my What is an exception? DevForums post.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

Swift code can't catch Obj-C exceptions and a swift app will crash if an Obj-C exception finds its way into swift code. See for instance:

Nothing has changed.

1 Like

Can you show a short self contained snippet reproducing the issue?