[swift-evolution] A unified error handling mechanism?

Anton Zhilin antonyzhilin at gmail.com
Sat Aug 6 10:40:31 CDT 2016

2016-08-06 2:50 GMT+03:00 Fernando Rodríguez <swift-evolution at 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”.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160806/9ca55dec/attachment.html>

More information about the swift-evolution mailing list