I've been reading up on how to properly handle (Option
, Try
and Either
) and define exceptions in Scala.
My question is: do exception hierarchies make sense in Scala? For example, we currently have a Scala project implementing something like:
class ServiceException(msg: String) extends Exception(msg)
class StorageServiceException(msg: String) extends ServiceException(msg)
class StorageServiceNotAvailableException(msg: String)
extends StorageServiceException(msg)
Is there an idiomatic way of defining such a hierarchy? Or maybe this concept is absorbed into something different altogether?
CONCLUSION FROM RESPONSES (see answer and comments below)
A hierarchy of exceptions makes sense if you want to recover from a non-leaf exception. Although, this doesn't prevent you from handling with Option/Try/Either
mechanisms which is more idiomatic than the traditional try/catch
.