August 2018
Intermediate to advanced
380 pages
10h 2m
English
The Try data structure is present in one form or another in many programming languages. It may contain one of two values. One possibility is an arbitrary type of value A, while the second is an exception. This data structure can be returned as the result of a computation that can result in an error. This way, you no longer need to throw an exception. You can just return Try[A] when your method may result in an error. In Scala, the square bracket after the type name stands for type parameters, so Try[A] means the type Try with the type parameter A.
For instance, consider an example where we have a function that divides one number by another. However, in the event that the second number is zero, we throw an exception:
def division(n1: Double, ...