I'm trying to figure out what methods/functions are going to
throw exceptions, and how to properly handle it, from a Swift
app.
Let me repeat my comment above: Objective-C exceptions are not recoverable. There is no way to properly handle them, even in Objective-C [1].
Maybe TKSmartCard is not following the guidelines about throwing exceptions?
… or you’re not calling it correctly. That’s why I asked for a crash report, so I can track down the throw site and work out what’s going wrong.
However, to be clear, Apple frameworks should not throw language exceptions that you’re expected to handle [2]. If one of our frameworks throws an exception, that’s the equivalent of a fatalError(…)
in Swift. The only correct behaviour is to crash the process.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] There are exceptions (hey hey) to this rule, when a class is documented to throw an exception in a very specific case. However, even that’s problematic in modern Objective-C because ARC doesn’t guarantee to reclaim memory when an exception is thrown. So, you can reliably handle an Objective-C exception if:
-
You’re programming in Objective-C.
-
You’re using MRR rather than ARC.
-
The exception is part of the API contract for the API you’re calling.
This is vanishingly uncommon in practice, and hence my generalisation.
[2] Except for the very few cases where the exception is part of the API contract. And we’ve been proactively removing those in recent years. Historically, the two most common ones were: