Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Either the equivalent to checked exceptions?

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

  • the possibility of failure is explicitly annotated in the method (throws or returning Either)
  • the programmer can handle the error case directly when it occurs or move it up (returning again an Either)
  • there is a way to inform the caller about the reason of the error

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

like image 368
Manuel Schmidt Avatar asked May 30 '12 14:05

Manuel Schmidt


People also ask

What is either type?

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").

What is the meaning of checked exception?

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.

Is runtime exception a checked exception?

Run-time exception is called unchecked exception since it's not checked during compile time.

Why are there no checked exceptions?

"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".


2 Answers

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 Eithers 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.

like image 125
Rex Kerr Avatar answered Sep 28 '22 11:09

Rex Kerr


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 value

Once 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.

like image 34
Don Stewart Avatar answered Sep 28 '22 13:09

Don Stewart