3

I know and understand the error handling in swift - no exceptions. But there are cases in Cocoa and Cocoa Touch that there is no other way to handle an error but catch an exception. For example if you write into file handle while you run out of free space. This is just not the situation when we want the app to crash... How to handle such a situation?

UPDATE

I see there is a little disturbance when Swift and exception are together in one sentence. So let me put emphasis on solution how to avoid crash on - writeData: if the only indication of "something wrong" (not runtime, program logic error) is exception thrown.

user500
  • 4,439
  • 6
  • 40
  • 54
  • 1
    possible duplicate of [Error-Handling in Swift-Language](http://stackoverflow.com/questions/24010569/error-handling-in-swift-language) – Undo Oct 16 '14 at 19:01
  • I see the answer too general. No space left - crash? Really? No solution for such a critical need? – user500 Oct 16 '14 at 19:14
  • Cocoa is not an exception-safe framework. As such, when an exception is thrown it is always *your fault*. This isn't Java where you should be catching and rethrowing exceptions willy-nilly. Swift makes that much more explicit by not having a try-catch mechanism. – CodaFi Oct 16 '14 at 21:37
  • 1
    False! In Java there are checked and unchecked exceptions. In Obj-C there should be only "unchecked" exceptions that crashes the app. "Checked" exceptions should be substituted by success/error pattern. But take a look at the API documentation of `NSFileHandle`s `- writeData:`. Typical example of checked exception - NOT MY FAULT AT ALL! My question is: How to avoid of crashing the app when I'm suddenly not able to write to a file? – user500 Oct 16 '14 at 22:42
  • Does this answer your question? [Catching NSException in Swift](https://stackoverflow.com/questions/32758811/catching-nsexception-in-swift) – Sindre Sorhus Mar 09 '20 at 14:48

2 Answers2

0

The current version of Swift doesn't have any exception-catching. Note that this might change in a future version.

MaddTheSane
  • 2,754
  • 22
  • 26
0

It looks like Swift 2 includes error handling with try/catch/throw - https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508.

Thanks to JonShier for the link.

Community
  • 1
  • 1
Chase Miller
  • 206
  • 1
  • 8
  • 2
    Swift 2's new error handling is not exception based, it just looks that way because of the syntax they adopted. – Jon Shier Jun 09 '15 at 05:19
  • @JonShier thanks for the correction (updated my answer). Do you have a link to read more about Swift's implementation and how it's different from exception-based handling? – Chase Miller Jun 09 '15 at 05:38
  • 3
    Apple's Swift 2 error handling documentation is here: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508 – Jon Shier Jun 09 '15 at 06:32
  • @JonShier - I'm not sure what you mean by "not exception based". It sure looks like an exception based system to me. Are you saying that Swift exceptions are not compatible with Objective-C exceptions? – Ferruccio Jun 09 '15 at 13:10
  • 4
    Swift *errors* are for user-relevant and/or recoverable errors, equivalent to ObjC APIs with `NSError` out-parameters. ObjC exceptions are, as in Swift 1.x, fatal errors if not caught in ObjC code, and have no direct equivalent in Swift. – rickster Jun 09 '15 at 15:01