[swift-evolution] A unified error handling mechanism?

Xiaodi Wu xiaodi.wu at gmail.com
Sat Aug 6 12:39:54 CDT 2016


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 at swift.org> wrote:

> 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”.
>>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160806/a457c239/attachment.html>


More information about the swift-evolution mailing list