Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sindresorhus opened this issue Jan 2, 2020 · 1 comment

Comments

@sindresorhus
Copy link
Member

  • Date: 2020-01-02
  • Resolution: Open
  • Area: Swift Compiler
  • OS: macOS 10.15.2
  • Type: Suggestion

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
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant