[swift-evolution] Shorthand for exhaustive catch statements

Simon Pilkington simonmpilkington at icloud.com
Sat May 6 15:16:42 CDT 2017


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170506/89a728fd/attachment.html>


More information about the swift-evolution mailing list