How to refactor this code combining IO and Either?

I think caching concrete exceptions can make sense sometimes.

Sometimes, exceptions work as “shared secrets” that communicate between layers of code without the intermediate code knowing about them. This might happen when some form of inversion of control is afoot, and there is some “framework” code that wraps “business logic” code both from above and from below.

For example: a persistence repository used by the business logic might throw an OptimisticLockingFailureException that bubbles up through the business logic, until it reaches some persistence framework handler that that catches that particular exception and decides whether to auto-retry the business logic. All of this without the logic itself knowing anything about how persistence is implemented.

Of course, this is not type-safe, in the sense that you might forget to catch the exception, nothing forces you to do it! But it’s often worth it compared to the alternative of polluting the signatures of all the intermediate functions.

I sometimes wonder if this exception strategy could be made more type-safe, not by using explicit sum types throughout the code, but with some separarate static analysis tool that found exceptions without any corresponding handler.

Sure, every rule of thumb has lots of exceptions. I’m pretty sure there are cases where even “checked exceptions” is actually the correct approach.

I never said catching concrete exceptions never makes sense. I actually remember I had to do it once (iirc it was some task scheduling code and it made sense to catch only sync exceptions but rethrow/abort on async ones). I’m just trying to argue, try being polymorphic allows for exactly that, while “listing” all exceptions in type signatures adds less benefit than issues it causes.

I think this is key.

As someone who once proposed adding checked exceptions, I think I’ve changed my mind because of this. I also dislike mtl-style effects because of the pollution of effects in the type signature.

I do still think there’s room for a UIO a type that represents IO guaranteed to not throw exceptions (e.g. FFI, IORef, etc.). But I think I agree that checked exceptions don’t fit with Haskell

It’s ambiguous what “having checked exceptions” would mean, but scoped exceptions give you the upsides without the downsides: Bluefin.Internal.Exception.Scoped

That is a cool technique, and it does solve error composition in that you simply pass two Exception e arguments to compose two exception types. But it still requires you to add the exception to all intermediate functions between try and throw. What upsides/downsides do you see here, other than error composition?

Exactly!

Yes, and I consider than an upside. I recognise that some people consider it a downside :slight_smile:

The main upside for me is that you can’t forget to handle something that you’ve thrown.

I don’t know how it plays with all the exception annotation stuff that’s been added recently. I haven’t looked into that deeply.

How does it compare with this

throw :: Throws e => e -> IO a

try :: (Throws e => a) -> IO (Either e a)

Is there a meaningful difference tracking at value level or type level?

I prefer value level since it’s more explicit, but other people like implicit. Other than that I can’t see a distinct difference.

A couple of comments:

  • The implementation in the linked blog post can only throw instances of class Exception, which isn’t necessary in my version, but I don’t think that’s required either.
  • In both cases the “capability” to throw can escape its scope. To avoid that you need to track scope, either with Bluefin (for the value-level version) or effectful (for the type level version).