• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 8 In Action: Exception Handling in Lambdas

 
Brad Collins
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Messrs. Urma, Fusco, and Mycroft:

Do you have any suggestions for handling checked (or even unchecked) exceptions in lambda expressions? Having to put a bulky try-catch block in a lambda expression seems to defeat the purpose. It just ruins the terse, functional style that lambdas make possible.

Scala has Try, and it helps. I haven't seen anything like that in the Java 8 API, so I've attempted to port the concept to Java 8:

https://github.com/bradleyscollins/try4j

Is there a better technique or paradigm already in Java 8 that I've overlooked?
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, checked exceptions are indeed something that lambdas do not facilitate. If the functional interface's method doesn't include the exception in its throws class, then you must catch the exception within the lambda.

Note that unchecked exceptions don't have this problem; the exception will simply be propagated to the calling code (which may catch it or propagate it further).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!