[swift-users] Every non-trivial Swift function should throw, right?

John Spurlock john.spurlock at gmail.com
Thu Mar 3 10:52:04 CST 2016

"so pretty much every non-trivial #swift function should throw, right?
 cheap & gives caller choice to catch, rethrow, try? or try!  (4 in 1)"
-- https://twitter.com/johnspurlock/status/704478619779866625

One of the annoying things about checked exceptions in other languages is
that they somewhat dictate client behavior wrt error handling and flow.
Also expensive (for some value of expensive) when they occur, so not a good
choice for standard-case control flow.

Given that Swift provides multiple language-standard ways for clients to
deal with a function marked as 'throws', it seems like almost all
non-trivial shared functions should provide the additional information of
the error in that standard form, instead of hiding it behind an optional
return type or a bespoke error callback argument.

i.e. always:  func parse(str: String) throws -> Foo
instead of: func parse(str: String) -> Foo?    // loss of information: why
did it fail?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160303/31ddc355/attachment.html>

More information about the swift-users mailing list