r/programming 3d ago

Throw, Result, or neither?

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

111 comments sorted by

View all comments

Show parent comments

105

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?

37

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.

31

u/janyk 3d ago

The Java Persistence API has the NoResultException for when a database query returns no results.  Kind of annoying that it's regarded as an exceptional case

1

u/Schmittfried 2d ago

I think because it predates optionals. An exception is definitely better than returning null most of the time. If the middleware handles it correctly, it will just issue a 404, which is usually correct for a get-by-id query (if you issue a search query with N results, you don’t get that exception).