in general there are two common ways to handle err...
# announcements
c
in general there are two common ways to handle errors: • throw exceptions, and catch them somewhere else • return a object that contains a successful value OR an error (e.g. with an
Either<>
class)
💯 2
not sure which is more "elegant". the second is more "functional" if you like that kind of thing
j
You can also implement the second with sealed classes :))
m
It all depends on your perspective, and objectives. The
Either
approach is more
functional
, and more explicit. This is a great approach for
expected exceptions
, meaning business logic/validation failures. The caller explicitly knows that what it's calling could fail in some way, and the ways are explicitly defined by the return value. It can also be cleanly handled, OR passed up the chain. Exceptions are good for critical failure, or unexpected failures where you generally don't want to clutter your code with handling mechanisms. Out of memory and other system failures are a good example of this. Personally, I go for this hybrid approach. They both have their place.
👍 2