Beginning in Scala and reading about Either
I naturally comparing new concepts to something I know (in this case from Java). Are there any differences from the concept of checked exceptions and Either
?
In both cases
throws
or returning Either
)Either
)I suppose one uses for-comprehensions on Either
to write code as there would be no error similar to checked exceptions.
I wonder if I am the only beginner who has problems to see the difference.
Thanks
The Either type is sometimes used to represent a value which is either correct or an error; by convention, the Left constructor is used to hold an error value and the Right constructor is used to hold a correct value (mnemonic: "right" also means "correct").
In broad terms, a checked exception (also called a logical exception) in Java is something that has gone wrong in your code and is potentially recoverable.
Run-time exception is called unchecked exception since it's not checked during compile time.
"Checked exceptions are bad because programmers just abuse them by always catching them and dismissing them which leads to problems being hidden and ignored that would otherwise be presented to the user".
Either
can be used for more than just exceptions. For example, if you were to have a user either type input for you or specify a file containing that input, you could represent that as Either[String, File]
.
Either
is very often used for exception handling. The main difference between Either
and checked exceptions is that control flow with Either
is always explicit. The compiler really won't let you forget that you are dealing with an Either
; it won't collect Either
s from multiple places without you being aware of it, everything that is returned must be an Either
, etc.. Because of this, you use Either
not when maybe something extraordinary will go wrong, but as a normal part of controlling program execution. Also, Either
does not capture a stack trace, making it much more efficient than a typical exception.
One other difference is that exceptions can be used for control flow. Need to jump out of three nested loops? No problem--throw an exception (without a stack trace) and catch it on the outside. Need to jump out of five nested method calls? No problem! Either doesn't supply anything like this.
That said, as you've pointed out there are a number of similarities. You can pass back information (though Either
makes that trivial, while checked exceptions make you write your own class to store any extra information you want); you can pass the Either
on or you can fold it into something else, etc..
So, in summary: although you can accomplish the same things with Either
and checked exceptions with regards to explicit error handling, they are relatively different in practice. In particular, Either
makes creating and passing back different states really easy, while checked exceptions are good at bypassing all your normal control flow to get back, hopefully, to somewhere that an extraordinary condition can be sensibly dealt with.
Yes, Either
is a way to embed exceptions in a language; where a set of operations that can fail can throw an error value to some non-local site.
In addition to the practical issues Rex mentioned, there's some extra things you get from the simple semantics of an Either
:
Either
forms a monad; so you can use monadic operations over sets of expressions that evaluate to Either
. E.g. for short circuiting evaluation without having to test the result Either
is in the type -- so the type checker alone is sufficient to track incorrect handling of the valueOnce you have the ability to return either an error message (Left s)
or a successful value Right v
, you can layer exceptions on top, as just Either
plus an error handler, as is done for MonadError
in Haskell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With