Sometimes we need to define a function that is always throwing exceptions (never terminating normally). Two real-life use cases are:
- Functions that simplify error throwing. This is especially useful in libraries where the error system is important and there is a need to provide more data about error occurrences. (As an example, look at the
throwError
function presented in this section.) - Functions used for throwing errors in unit tests. This is useful when we need to test error handling in our code.
For these kinds of situation, there is a special class called Nothing
. The Nothing
class is an empty type (uninhabited type), meaning it has no instances. A function that has Nothing
return type won't return anything and it will never reach the return
statement. It can only throw an exception. This is why when we see that a function is returning Nothing
, then it is designed to throw exceptions. This way we can distinguish functions that do not return a value (such as Java's void...