How to ignore errors?

First of all, let me just go out and say that this new error handling system in Swift is a huge, huge improvement. However, this is one case that I'm unsure about, which is when you legitimately care about errors. However, sometimes you don't care about the specific error, but you just want to know if something succeeds or not. A good example is checking the availability of a file, where you just want to know if it's reachable and don't want to bomb out if it's not. In Objective-C, this would be done with one line of code, simply by passing NULL for the error parameter:


BOOL isReachable = [someURL.checkResourceIsReachableAndReturnError: NULL];


However, in Swift 2, the best way I can see to do this is:


let someURL = ...
let isReachable: Bool

do {
     try someURL.checkResourceIsReachableAndReturnError()
     isReachable = true
} catch {
     isReachable = false
}

This seems somewhat verbose and awkward. Is there any more elegant way just to check if a file is reachable without treating the result as an error?

Grammar correction: "However, there is one case." Is there any way to edit posts with the new board software?

Additional correction: "checkResourceIsReachableAndReturnError()" on line 5 should be just "checkResourceIsReachable()".

In the Swift 2 Prerelease for the Swift book, it states:


“There are some cases in which you know a throwing function or method won’t, in fact, throw an error at run time. In these cases, you can call the throwing function or method in a forced-try expression, written, try!, instead of a regular try expression.


Calling a throwing function or method with try! disables error propagation and wraps the call in a run-time assertion that no error will be thrown. If an error actually is thrown, you’ll get a runtime error.”


https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508(there is no way to deep-link to the relevant section but it's the second from last, "Disabling Error Propagation.")


If you have a chance to watch the video posted on the developer site from the "What's new in Swift" session, it's well worth it. I think that's where I saw this.

>>Is there any way to edit posts with the new board software?


There's an Actions menu at the bottom left of the post.

Seems like it would be trivial to write your own function that takes a block and returns whether an error occured or not.

Something like this, untested

func didError(closure: () throws -> ()) -> Bool {
  do {
    try closure()
    return false
  }
  catch {
    return true
  }
}

Yikes, yeah, that is annoying. I feel like they need a plain `- (BOOL)isReachable;` now.


As a workaround, you could try using the NSFileManager:fileExistsAtPath instead, but this is very specific to this case.


It looks like they *really* don't want you ignoring the errors any more. I suppose we'll start seeing lots of class extensions that do something like this:


extension NSURL {
     func checkResourceIsReachableAndIgnoreError() -> Bool {
          do {
               return try checkResourceIsReachable()
          }
          catch {
               return false
          }
     }
}
How to ignore errors?
 
 
Q