I really like the points made in this article about Swift error handling: using throw
/try
/catch
is actually much better than returning optionals or the new Result
type.
👉Benefits of using throwing functions (try) – Swift’s most underrated feature?
Throwing errors, when used throughout your codebase, helps you reduce and simplify code. It makes your unit testing easier too.
As a former Java programmer, I have to admit some hesitance to throw
anything. Java error handling can turn into a nightmare of its own.
But the author makes some compelling arguments why throw
ing might make sense in Swift. Error handling is never fun; let’s do it the easy way. 🤷🏻♂️
In a related note, Re: Making Wrong Code Look Wrong also talks about the benefits of throwing in Swift, in particular with relation to local reasoning. 👍
Via iOS Dev Weekly.