[swift-evolution] Shorthand for exhaustive catch statements

Xiaodi Wu xiaodi.wu at gmail.com
Sat May 6 15:26:52 CDT 2017


_If_ this is something that's important, then the ! should follow 'catch'
and not 'try'.

The keyword 'try!' already means something, and it's not that. People may
already mix try! and try in a do block and the behavior cannot be changed
for source compatibility.
On Sat, May 6, 2017 at 15:16 Simon Pilkington via swift-evolution <
swift-evolution at swift.org> wrote:

> As a number of recent threads have shown, typed throws and error handling
> is a complicated topic with a lot of implications.
>
> However in the mean time we could potentially address one of the pain
> points people give for wanting typed throws - exhaustive catch statements.
>
> For functions that you completely control, it is possible to be completely
> certain of the errors the function will throw and it is an unrecoverable
> logical error for any other error to be thrown. Currently this situation
> would have to be handled like so-
>
> do {
>     try throwingFunction()
> } catch MyError.TheError(let theContext) {
>     // do something
> } catch {
>     fatalError(“Impossible error?")
> }
>
> Here, the catch-all is just boilerplate to make the catch statement
> exhaustive but doesn’t provide any value to the programmer. Would it make
> sense to provide a shorthand for this case like this-
>
> do {
>     try! throwingFunction()
> } catch MyError.TheError(let theContext) {
>     // do something
> }
>
> Where the try! still indicates the call could fatalError if the function
> throws an unmatched error but allows the programmer to specify the errors
> that they specifically want to handle. I don’t think this moves the
> language away from the possibility of typed throws; adding typed throws
> would likely cause a warning in both cases - either to remove the catch-all
> or all the ! - if the compiler could determine the catch was now exhaustive
> without them.
>
> If this has been proposed elsewhere I apologise.
>
> Cheers,
> Simon
> _______________________________________________
> 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/20170506/e9260a21/attachment.html>


More information about the swift-evolution mailing list