Skip to Content
Functional Programming in Java
book

Functional Programming in Java

by Venkat Subramaniam
February 2014
Intermediate to advanced
160 pages
4h 59m
English
Pragmatic Bookshelf
Content preview from Functional Programming in Java

Dealing with Exceptions

Java programmers are quite opinionated about checked exceptions. Irrespective of how we feel about them, checked exceptions are here to stay and we have to deal with them. Let’s look at some options for working with them in the context of lambda expressions.

In the next example we create a lambda expression that invokes a method that potentially throws a checked exception. We take a list of path names and ask for their canonical path using the getCanonicalPath method.

 
public​ ​class​ HandleException {
 
public​ ​static​ ​void​ main(​String​​[]​ args) ​throws​ IOException {
 
Stream.of(​"/usr"​, ​"/tmp"​)
 
.map(path -> ​new​ ​File​(path).getCanonicalPath())
 
.forEach(​System​.out::println);
 
//Error, this code will ...
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

Functional Programming in Java

Functional Programming in Java

Pierre-Yves Saumont

Publisher Resources

ISBN: 9781941222690Errata Page