In the November 2017 SPP meeting, one of the questions raised was about how Platform libraries should handle failures of various sorts. It was asked originally about better-files, which generally throws exceptions for IO failure, and though good reasons were given why better-files in particular should keep its behaviour (because it is meant to simply wrap Java’s API), the question remains for other Platform libraries. Should they throw exceptions? Should they return
Eithers, or some other functional construct? Some combination of the different approaches?
I think it is worth noting that both approaches have legitimate uses. If you expect whatever operation you are performing (e.g. reading a file) to always succeed, throwing exceptions is both simpler and cleaner to code, and easier to debug. Having to deal with an
Either to handle results - or potentially several (if you are performing many IO operations) - can be extra work, as well as unnecessary noise in the code. On the other hand, if you expect the operation you are performing to fail in normal circumstances, throwing exceptions for every failure is undesirable (even wrapped in a
Try, because filling in stack traces is expensive). For expected failures, having a functional construct such as
Either can make it significantly easier to handle many results: whether by filtering out failures, mapping one result type, or splitting attempts into different collections for successes or failures.
What approach should Scala Platform libraries follow?