Stop Throwing Error: Typed Throws in Swift 6
How making errors explicit improves readability, safety, and API design
How angry 😤 would you be if you tried to understand what a function does and it returned Any?
func nextScreen() -> AnyWhen you see a method, you usually have a few seconds to understand it at a high level based on its signature. You look at:
the name, scope
the parameters
the return type (+ whether it’s async or not 😅)
The problem with throws
Traditionally in Swift, a throwing function looks like this:
func makeSantaGift(kidName: String) throws -> GiftBut the function signature doesn’t tell you. The error type is effectively any Error 😩
Typed throws to the rescue 🎄
Starting with Swift 6.0, Typed Throw jumped in.
Function’s signature is enhanced (1) (2) with more information.
The compiler now knows all possible error cases. And exhaustivity is the best part 👇

It's still a mystery to me why compiler is not able to recognize that all enum cases have been considered. 🤔💭That means if the library adds a new error case in the future, your code will fail to compile until you handle it.Personally, I think Typed Throws significantly reduce the time it takes to understand what a function does without opening its implementation. Making it ideal for libraries and generally units/functions of high fan-in.
Have you ever used this in your company? If not, I’d love to hear the reasons drop a comment in the discussion section 👇
And since this is probably my last post of the year…
See you in the next article and Happy New Year 🥳
Konstantinos Nikoloutsos




