29

Is it possible to catch exceptions in Swift? Given the following code:

NSException.raise(NSRangeException,
    format: "Now you've gone too far!",
    arguments: CVaListPointer(fromUnsafePointer: UnsafePointer()))

Is it possible to prevent the exception from crashing the entire program? That is, what is the Swift equivalent of the following in Objective-C:

@try {
    [NSException raise:NSRangeException format:@"Now you've gone too far!"];
}
6
  • this answer may also help: stackoverflow.com/a/24010741/1249958 Commented Jun 3, 2014 at 19:21
  • Sorry, should have searched more diligently before submitting a duplicate question! Commented Jun 3, 2014 at 19:29
  • 2
    You can add try-catch support for Swift by following the instructions in this article: medium.com/@_willfalcon/adding-try-catch-to-swift-71ab27bcb5b8 Commented Oct 13, 2014 at 3:50
  • @WilliamFalcon, put it as an answer. Sometimes you have to interact with ObjC library throwing exceptions, your approach seems the only way. Commented Feb 10, 2015 at 16:58
  • 1
    it is possible in swift 2.0 Commented Jun 10, 2015 at 11:23

2 Answers 2

12

It doesn't have exception handling, and this discussion in the developer forum discusses why it may be so:

but keep in mind that Cocoa and Cocoa Touch traditionally don't intend for you to catch exceptions; they intend for you to not cause them to be thrown in the first place. Ordinary errors should be handled with optional types and inout NSError parameters; you should address any situation that causes an assertion to fail (which seems to be the only exception-throwing mechanism in Swift) by writing better code.

Sign up to request clarification or add additional context in comments.

6 Comments

That's very unfortunate. I'm aware that try-catch was never really encouraged in Objective-C, but currently there's no way to unit test whether Swift code throws an exception. XCTAssertThrows is not available in .swift tests, and without try-catch, there's no way to write a passing test that involves exceptions.
That comment from the forums is wrong. Foundation throws exceptions in a number of places that need to be caught for the program to function normally. The best example is writing to an NSFileHandle when the NSFileHandle experiences a broken pipe (e.g. a server writing to a connection after the client has closed the connection). There is no other way to detect this condition other than writing to the connection and it is a valid, non-fatal scenario. Without exception handling, Swift programs cannot implement server-like functionality using the Cocoa APIs.
I find it quite disturbing to throw error from the API, and don't let the swift language handle them. I just experienced the situation and really don't appreciate it. Sorry to be non-constructive, but some things need to be said
Swift 2 appears to now have this feature.
|
0

I believe that, as of today, Swift does not support this. It will most likely be added on future betas.

4 Comments

Do you have any data to back up your last sentence, or is it a hunch?
Basing my answer on the same documentation that manjolds posted on here: stackoverflow.com/a/24023248/1011787. Cocoa and CocoaTouch traditionally don't intend for you to catch exceptions, however they're still supported. That's why I said "most likely" — even though they don't encourage their use, they still provide that option.
probably, you assumption about "It will most likely be added on future betas." is not correct according to the official docs, because they clearly indicate that it won't happen in the future either. you can throw exception in Swift, but it cannot be caught in runtime therefore those are always critical exceptions.
this is no longer correct, you might want to update your answer to capture possible votes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.