

We will 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. It is easier to implement, compose, and transform. Instead of using try-catch blocks all over the code, we should work with to handle all the exceptions in the code. In order to define the correct message for the exception that might occur, you might want another try-catch block for different exception cases.
#SCALA TRY CATCH CODE#
your code would determine what kind of exception message will be sent in the Left instance and what return value you want in the Right instance.

#SCALA TRY CATCH HOW TO#
If (input.i threshold) Right(Output(input.It’s time to continue our previous blog on how to make our code more robust, concise, and more functionally defined at the same time. Val max: Int = failureThreshold: Double = timeFactor: Int = baseTimeTokens: Int = _ĭef getSampleInput: Input = Input(Random.nextInt(max)) Sealed trait ThisIsError extends Product with SerializableĬase class Invalid(input: Int) extends ThisIsErrorĬase class UhOh(reason: String) extends BenchmarkState validInvalidThreshold: Int = _ So, let’s start with: case class Input(i: Int) This pattern is generic enough to be applicable in a wide variety of circumstances – e.g., authentication and calling external services – and allows for the measurement of the impact of various techniques – e.g., EitherT and exceptions – without being restraining. If it isn’t, the code performs some bookkeeping and propagates the error back to the caller.

If the result is correct, the code performs some additional processing and returns the final result. Once validated, the code kicks off a transformation, fetches additional “data,” calls the outside world, and waits for a result. This usually means a code that takes some sort of data (an input) and validates it. Which, in this case, means a typical code that deals with error-handling in business logic. The first step is to create a piece of code that’s representative of the problems you want to measure.

So let’s try to go beyond these hunches and try to measure the impact of being uncompromising functional programmers. It’s then that whatever you are doing in your code is mostly irrelevant because communication costs dwarf any benefits or losses. You may as well throw in the towel and rewrite everything in Kotlin.Īnother gut feeling you might have happens when your code starts calling other systems via network. On the other hand, if you stop using the benefits of functional abstractions made possible by Scala’s powerful type system, then you’re left with just a “better Java” kind of language. Yes, the gut feeling every Scala developer has is that all the fancy monadic transformers add a lot of non-optimizable indirection (at the bytecode level) that throws JIT off and is slower than what your Java colleagues might have written. So, should I abandon it and write error handling myself? But if I do that, isn’t the pattern-matching slow? Meaning the best solution would be to simply throw exceptions? I have used EitherT all through my code base because it helps with concise error handling. The question I’ve been hearing a lot recently is: So, should practitioners of FP drown in guilt for writing inefficient code? Should they give way to less functional code? Yet, these abstractions are an immense help if you want to write clean and abstract code. Conventional wisdom has it that using too many functional abstractions in Scala is detrimental to overall program performance.
