Skip to content

Instantly share code, notes, and snippets.

@mariofusco
Last active January 2, 2016 10:09
Show Gist options
  • Save mariofusco/8287989 to your computer and use it in GitHub Desktop.
Save mariofusco/8287989 to your computer and use it in GitHub Desktop.
import org.junit.*;
import static junit.framework.Assert.assertEquals;
import static org.javaz.Try.attempt;
public class TryTest {
public String first(boolean throwEx) throws Exception {
if (throwEx) throw new RuntimeException("Cannot read first");
return "6";
}
public String second(boolean throwEx) throws Exception {
if (throwEx) throw new RuntimeException("Cannot read second");
return "2";
}
@Test
public void test() {
Try<Integer> result = attempt(() -> first(false))
.map(Integer::parseInt)
.flatMap(first -> attempt(() -> second(false)).map(Integer::parseInt)
.map(second -> first / second));
assertEquals(3, (int)result.orElse(-1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment