Back2Basics: Exception Handling – #4

Table of contents
Reading Time: 3 minutes

In the previous series of blogs, we have seen how we can do error handling in Scala. In this blog, we will explore yet another way of handling exceptions using the functional style of programming.

Scalactic is a library which provides an “Either with attitude” named Or, designed for functional error handling. Or gives you more convenient chaining of map and flatMap calls (and for expressions) than Either.

Or is a class in the Scalactic library which represents a value with two possible types, one type being “good” and the other one being ”bad

One can wonder why to use Or when Either is such a wonderful feature provided with ease of writing and understanding as we wrap error in Left() and correct execution flow in Right(). But the thing with Either is that it treats both its Left and Right alternatives identically. It is by convention that Right should contain the valid result and Left should contain the error.

It was before Scala 2.12 was released, Or had more powers than Either as we could not perform monadic operations on Either directly, first Either has to be projected using left or right and then can be transformed using map or flatMap. But after the release of Scala 2.12, monadic operations could be performed directly on Either just like Or.

Let us define a method that parses name and another method that tells if the voter is eligible to vote or not:

Let us now see if the voter can vote or not using method canVote

We can also use Either instead of Or, as mentioned above, they both have same powers so one can use Either or Or as preferred. Let us now call canVote method:

which will result in:

we can easily perform map operation on it.

If we provide bad value,

Result will be

In the above method, canVote, method calls are chained after one another, if parseName method returns a Bad value, eligibleToVote method will not be called, so, what do we do to report all the errors that are there?

Let’s say, we call canVote method with the following values:

Here only one error is displayed, but what do we do if we want all the errors that are there in the input values. Scalactic provides features to accumulate errors.

We can accumulate errors with Or. It is one of the differences between Either and Or. Or enables you to accumulate errors if the Bad type is an Every. An Every is either a One, which contains one and only one element, or a Many, which contains two or more elements.

To accumulate the errors, we need to rewrite the methods, as the return type of Bad type have to be changed to Every. Let’s rewrite the methods:

Now we can accumulate error using withGood method

withGood method on Accumulation, applies the good values to the given function, here the name and age which you would provide will be applied in {…} and it will return the result wrapped in a Good otherwise it returns a Bad containing every error ie. a Bad whose Every includes every value that appears in any Bad.

So, calling,

will return :

This is how we can accumulate errors using Or.

Feel free to suggest or comment.




Written by 

I am a Software Consultant and has experience of more than 1.5 years. I like to study and write about latest technologies.

3 thoughts on “Back2Basics: Exception Handling – #44 min read

  1. IMHO Or type has confusing name contrary to Validated (cats library) which name clearly describes its purpose. On other hand Either is not a type for errors or validation, it’s so called Sum type to represent situation when you could get one or another type but unrelated ones (Or name suits for it to), and yes it could be used to represent errors but not only that. So I would recommend to use Validated for errors handling because of its more obvious semantics and cleaner syntax without trying to provide both fail fast and accumulation syntax.

Comments are closed.