Typed throws have been in high demand ever since Swift gained the throws
keyword. The error values themselves are always type-erased to any Error
. This approach encourages errors to be handled generically, and remains a good default for most code.
Finally this feature was introduced in proposal SE-0413 and implemented in Swift 6.0
Typed throws provides several benefits:
any Error
Errors are usually propagated or rendered, but not exhaustively handled, so even with the addition of typed throws to Swift, untyped throws is better for most scenarios.
throws
with a specific error typeenum AppError: Error {
case invalid
case network
}
func foo() throws(AppError) {
// ...
}
throws
with any Error
is equivalent to untyped throwsfunc foo() throws(any Error) { ... }
func foo() throws { ... }
throws
with Never
is equivalent to a non-throwingfunc foo() throws(Never) { ... }
func foo() { }