Fernando, the rationale behind the design of error handling in Swift is detailed here: https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst Some of that document may now be out-of-date, but you may find it to be helpful nonetheless.
On Sat, Aug 6, 2016 at 10:40 AM, Anton Zhilin via swift-evolution < swift-evolution@swift.org> wrote: > 2016-08-06 2:50 GMT+03:00 Fernando Rodríguez <swift-evolution@swift.org>: > > b) do/try/catch >> >> This allows you to have information about the error, but also causes the >> newly created object to be "trapped" inside a do block. >> >> Are there any plans to address this situation? I believe there should be >> a single, obvious and convenient way of handling errors in the language. >> > func attempt<T>(_ expression: @autoclosure () throws -> T, handler: > (ErrorProtocol) -> T) -> T { > do { > return try expression() > } catch { > return handler(error) > } > } > let x = attempt(downloadDataOrThrow()) { > print("Connection lost") > return nil > } > > It mimics guard statement. You can also have versions of attempt with > partially defined handlers, like “return nil“ or “print and crash”, or > “assert type of error”. > > > _______________________________________________ > swift-evolution mailing list > swift-evolution@swift.org > https://lists.swift.org/mailman/listinfo/swift-evolution > >
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution