My take on the general problem is that it depends on where the keys are coming from.
If they are being entered by some user or untrusted system, then I use Option
so I can meaningfully denote the possibility of an unknown key and deal with it appropriately.
On the other hand, if the keys are coming from a known system (this includes things like keys embedded in links that originally came from the system), and are assumed to be valid and exist, I would leave it as a runtime exception, handled by a catch-all at the outer level. For the link example, if someone manually changes the key in a url for one reason or another, it should be considered as undefined behaviour and an exception is appropriate, IMO.
Another way to think of it is how you would handle the situation when it arises. If you're using Option
and are just delegating the None
case to some catch-all error handling, then an exception is probably more appropriate. If you're explicitly catching the NotFound exception and altering the program flow (eg, asking the user to re-enter the key), then use Option
, or a checked exception (or Either
in Scala) to ensure that the situation is dealt with.
In relation to integrating with Java, Option
is easy enough to use from there once the Scala runtime library is available on the classpath. Alternatively, there's an Option
implementation in the Functional Java library. In any case, I would steer clear of using null
to indicate "not found".