Hey everyone, it’s time to continue our previous blog on how to make our code more robust, concise and better functionally defined at the same time. Here we are moving more towards Scala and leaving traditional Java behind. We will see new types and their usage along with the benefits we get from them.
This blog will show you the best way in my perspective to be used in order to handle the exceptions gracefully in Scala ( The beloved coding language for everyone here at Knoldus ) . Our methods would only be responsible for either returning the results or the exceptions, i.e. we will not write any try-catch blocks to handle the exceptions within different methods.
You might be thinking by now that we will be talking about the Either type in Scala next to achieve the composition of methods. Although we can do that but its quite different then the desired functionality and we would not use it here due to the following 2 reasons :
- Either type works when you are explicitly defining what will be kept within Left (generally depicting failure) or Right (generally depicting success), i.e. your code would determine what kind of exception message will be sent in the Left instance and what return value you want in Right instance.
- In order to define the correct message for the exception that might occur, you might want another try-catch block for different exception cases.
Instead of using try-catch blocks all over the code, we should rather work with scala.util.Try to handle all the exceptions in the code. As it is easier to implement, compose and transform. We would see the usage of Try in this block but we are only looking at the ways of handling exceptions in methods that are sequential in nature. If we need to work on concurrent methods that may run on different threads simultaneously, Futures in Scala provides direct exception handling support via recover.
Instead of wrapping your code in a try-catch block, just wrap it within a Try block.
Now to explore further the advantages to use Try instead of try-catch block consider the following example :
In the above code we are composing different methods together using transformations on Try type. It removes all the boilerplate try-catch blocks and also allows you to handle all exceptions at one place. If the method riskyCodeInvoked is causing an exception control will not go to anotherRiskyMethod and so forth.
If you are more comfortable with Option type, you can directly use Try like this :
There are other methods also which are defined on Try type, like recover, getOrElse, orElse, isSuccess, isFailure, Filter etc. Once you get used of it, you might not want to use try-catch blocks ever 😉 so try it out in your code and enjoy the simplicity of Scala.