Despite checked exceptions being generally bad practice and anti-pattern, I find one feature very useful: having list of checked exceptions part of function signature.
Checked exceptions are "business exceptions", something that requires from the source code or user to do something to recover and continue normal functioning. Having possible checked exceptions as a method signature gives me compile time type safety, and gives me list of possible outcomes after calling a function.
I struggle to find a proper alternative to this. Generally, the proposed alternative is using Either
, or Try
, or Result
, or some similar class/structure that may keep either happy path result, or an Exception instance that marks a business exception that happened. But these solutions does not help ne knowing what are possible exceptions that I should expect, so that I can choose how to react in which situation.
There is an option to list possible exceptions in the javadoc/kdoc, but it may not be truth, only the code is truth, after all and I want my type system and compile time safety.
Since these exceptions are not really thrown, so instead of them we could use enums, each function call having it's own enum specify possible alternative scenario outcomes, but that seems little bit overwhelming, and I don't see such thing being proposed.
Is there any widely accepted solution to this problem, that is according to some of the accepted best practices?