Many Cocoa APIs still throw Objective-C exceptions at runtime. These are impossible to catch in Swift. There are ways to do it by creating an exception catch handler in Objective-C and then use it in Swift, but I prefer to not have any Objective-C code in my projects.
The API could be something like this:
do {
try ObjC.catchException {
// Call something here that might throw an NSException.
}
} catch {
print("An error occurred: \(error)")
}
Description
Many Cocoa APIs still throw Objective-C exceptions at runtime. These are impossible to catch in Swift. There are ways to do it by creating an exception catch handler in Objective-C and then use it in Swift, but I prefer to not have any Objective-C code in my projects.
The API could be something like this:
As you can see from the votes on this Stack Overflow question, it's a common need: https://stackoverflow.com/questions/32758811/catching-nsexception-in-swift