# Why Try - Java 8 lambdas and checked Exceptions

DevFeed: [Why Try - Java 8 lambdas and checked Exceptions](<https://devfeed.tech/articles/why-try-java-8-lambdas-and-checked-exceptions-32001.md>)

Original publisher: [Read original article](<https://tech.finn.no2016/01/20/why-try-java-8-functional-programming/>)

Author: Sjur Millidahl

Published: 2016-01-20T07:13:00Z

Content type: tutorial

Language: en

Sources: [Finn.no](<https://devfeed.tech/sources/finn-no.md>)

Topics: [Java](<https://devfeed.tech/topics/java.md>), [exceptions](<https://devfeed.tech/topics/exceptions.md>), [Scala](<https://devfeed.tech/topics/scala.md>), [Open Source](<https://devfeed.tech/topics/open-source.md>)

Tags: [compiler](<https://devfeed.tech/tags/compiler.md>), [exceptions](<https://devfeed.tech/tags/exceptions.md>), [functional](<https://devfeed.tech/tags/functional.md>), [java](<https://devfeed.tech/tags/java.md>), [lambda](<https://devfeed.tech/tags/lambda.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [scala](<https://devfeed.tech/tags/scala.md>)

## AI overview

This tutorial explains how checked exceptions complicate Java 8 lambdas. It presents FINN.no's open source Try structure, inspired by Scala's Try, to represent computations as Success or Failure and simplify mapping, flat-mapping, and recovery.

## Source excerpt

Why won't this compile? (And why does riskyTask throw a PrinterException?!) One does not have to use lambdas in Java 8 long before running into the obstacle of checked Exceptions. Because the PrinterException is checked, the compiler forces us to deal with it, even within a lambda: private void demonstrate() { IntStream ones = IntStream.generate(() -> 1); ones.map(i -> { try { return riskyTask(i); } catch (PrinterException e) { System.out.println( "Your printer is out of ink or laser beams!" ); } return i; }); } private int riskyTask(int a) throws PrinterException { return 2; } But we don't like to do this. We use lambdas to express intent in a concise and elegant fashion. The try/catch-brackets feels like noise. For this reason, FINN.no's open source lambda-companion project introduces a useful structure for using lambdas in a world with checked Exceptions : Try. A Try represents a computation which might fail, and is always represented as a Success or a Failure (but never both). The concept borrows from Scala's Try, and shares several properties to other monadic functional structures : Future (completable success or failure) Optional (present or empty value) Either (one of two values) FINN.no's Try is right-biased, meaning that one can map and flatMap on a Try without having to add specific logic to handle a Try being a Failure; the computation will simply only take place if it is a Success. Using a Try we could refactor our riskyTask-example: private void demonstrate() { IntStream ones = IntStream.generate(() -> 1); Stream<Try<Integer>> tryStream = ones.mapToObj(this::riskyTask); tryStream.forEach(this::print); } private Try<Integer> riskyTask(int a) { return new Success<>(2); } private void print(Try<Integer> t) { Integer defaultNumber = 0; Integer i = t.recover(success -> success, failure -> defaultNumber); System.out.print(i); } This is contrived of course. And one should rarely accept a Try as a method argument. (And one should rarely generate an infinite str