Software Dev

Learn to Love Throwing in Swift

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 throwing 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.