Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Functional interface and exception  RSS feed

 
Linwood Hayes
Ranch Hand
Posts: 40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question about functional interface and lambda:   suppose we have



The above function interface means to open a file and do something within certain chars or length..   In my lambda code, I would do things like



Now, since it works with File, compiler would require you to catch certain exceptions..  But in the function interface, do we need to throw any Exception ?  You know, sometimes the exception is really related to individual implementation, i.e. different lambda impl can generate diff exceptions.  So, should we "throws..." in interface method at all ?

thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37044
505
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THis is one of the annoying thing with lambdas. You can't throw checked exceptions. So instead you need to have the implementation catch the checked exception and rethrow as a runtime exception.
 
Linwood Hayes
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne.  I can understand that I let  the code that enclosing lambda implementation do the try/catch stuff.   But I don't understand why I need to re-throw it as Runtime exception ?  why can't I just handle the checked exception ?
 
Linwood Hayes
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linwood Hayes wrote:Thanks Jeanne.  I can understand that I let  the code that enclosing lambda implementation do the try/catch stuff.   But I don't understand why I need to re-throw it as Runtime exception ?  why can't I just handle the checked exception ?


Make it little clearer what I meant



Is this OK and good practice ?
 
Paul Clapham
Sheriff
Posts: 22266
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if your code can reasonably handle the exception (as opposed to just catching it and ignoring it) then yes, you can do that. But if a normal design would allow the method to throw that exception because it can't handle it, then your lambda's design should throw it too -- as a runtime exception.
 
Rob Spoor
Sheriff
Posts: 21019
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lambdas can throw checked exceptions, if the functional interface has defined it. For instance, if you tweak the extractFileData method a bit, it is now allowed to throw IOException:

I actually got so annoyed by the number of times I had to catch IOException and wrap it (in an UncheckedIOException, the best wrapper for IOExceptions since Java 8), I wrote a copy of all interfaces in java.util.function: https://robtimus.github.io/io-functions/. All can quite easily be converted to their equivalent in java.util.function using the provided unchecked methods.

I've also written an SQLException equivalent at https://robtimus.github.io/sql-functions/, but I doubt that will be used often.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!