7

Given a method in UserService: update, what's the best way to handle errors/exceptions here?

Option A:

def update(...): Try[User]

In this way, I need to define my custom exceptions and throw them in the function body when needed. Most of these exceptions are business errors (e.g. user_id cannot be changed, etc). The point here is no matter what exception(s) are thrown (business error, network exception, DB IO exception, etc), treat them the same way and just return a Failure(err) - let the upper layer handle them.

Option B:

def update(...): Either[Error, User]

This is the exception-free way. In the function body it catches all possible exceptions and turns them into Error, and for business errors just return Left[Error].

Using Try seems to be a more natural way to me as I want to handle errors. Either is a more generic thing - Either[Error, T] is just one special case and I think Try is invented for this special case. But I also read that we should avoid using exceptions for error handling...

So, which solution is better, and why?

1
  • 1
    I'd prefer Either, because it allows you to be explicit about which error types are possible, rather than the generic Throwable you get with Try. As @Gabriele says, I'd prefer scalaz \/ as it has a more usable map construct.
    – lmm
    Commented Nov 27, 2014 at 14:08

1 Answer 1

8

There's no silver bullet.

As you noted already, Try is simply a more specialized version of Either, where the Left type is fixed to Throwable.

Try might be a good fit if you need to materialize exceptions thrown by external (perhaps java) libraries, as its constructor automatically catches them.

Another advantage of Try is that it has map and flatMap, so you can use it directly in for-comprehensions, whereas with Either you would have to explicitly project on the right case. Anyway, there's plenty of alternative implementations with a "right-bias", and probably the scalaz \/ type is the most popular one.

That being said, I typically use \/ or the almost equivalent Validation (both from scalaz), as I like having the ability of returning errors that do not extend Throwable.

It also allows for more precise error types, which is a huge win.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.