feedback-assistant / reports

Open collection of Apple Feedback Assistant reports
213 stars 2 forks source link

FB7517603: Add ability to catch Objective-C runtime exceptions in Swift #74

Open sindresorhus opened 4 years ago

sindresorhus commented 4 years ago

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:

do {
    try ObjC.catchException {
        // Call something here that might throw an NSException.
    }
} catch {
    print("An error occurred: \(error)")
}

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

sindresorhus commented 4 years ago

Swift package for this: https://github.com/sindresorhus/ExceptionCatcher