Skip to Content
Mastering Functional Programming
book

Mastering Functional Programming

by Anatolii Kmetiuk
August 2018
Intermediate to advanced
380 pages
10h 2m
English
Packt Publishing
Content preview from Mastering Functional Programming

Try

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781788620796Supplemental Content