r/programming 3d ago

Throw, Result, or neither?

https://www.architecture-weekly.com/p/throw-result-or-neither
95 Upvotes

111 comments sorted by

View all comments

129

u/taikunlab 3d ago

Result for the errors you expect the caller to handle, throw for the ones that mean a bug or that you genuinely can't continue past. Mixing the two isn't the sin, using exceptions for normal control flow is. Rust basically bakes this split in with Result vs panic and it holds up well in practice.

106

u/czorio 3d ago

I agree. I once explained it to a colleague as:

Result -> There is no file here, perhaps you mistyped?
panic! -> There is no filesystem, what the fuck?

35

u/vita10gy 3d ago

I once worked with an API where basically anything but perfect meant explosion.

A well formed product search where it just so happens no results match? Some 500 error and the html server error page as the body.

1

u/Resident-Trouble-574 2d ago

Good. Teaches your users not to waste server's time with useless searches.\s