Michael Long
1 min readAug 5, 2024

--

Sorry, in the try/catch camp.

Returning errors as values during an API load now means you're not only forced to handle the error inline, but now you need some other mechanism (inout variables, reference types) to return the actual data desired.

Make a second API call and you need to do the same.

Decode that value and now you need to handle that potential error.

Placing those calls in a try catch block and each function returns the desired value directly, the sequence is clear and easy to understand, and error handling is handled in one place and one place only..

Fortunately, we don't have to debate the issue in Swift/iOS, since Apple already decided that URLSession data calls and JSONDecoder calls all throw, which means we might as well conform to the platform standard since we're going to need to catch those errors anyway..

Might also mention that Combine/RxSwift fundamentally "throw" errors across operators once an error occurs... which is one thing I always liked about them.

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

No responses yet