Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

forEach can't handle Exceptions

 
Bartender
Posts: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abbreviation of my code:
It's more complicated than this but the problem  I'm running into is that "someMethod()" is declared to throw an SQLException and Eclipse insists that the method call should be wrapped in a try/catch block. My enclosing method is already declaring that it throws an SQLException and I don't want to catch it.

Is there a way to handle this situation? I can do it with a non-streaming for() loop but I was hoping to use Streams.
 
Marshal
Posts: 69847
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of question suggests to me that there is a disconnect between the concept of checked exceptions, and the structure of a stream; a proper functional program shouldn't throw any exceptions at all because they constitute side‑effects. Oracle have had a go at that problem with the introduction of this!
What do you want to happen if an exception is thrown? Can you ignore Eclipse's warning? Can you swap the exception for an unchecked one?
 
Saloon Keeper
Posts: 12154
258
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why force it? You're using streams, but you're still doing procedural programming. There's nothing sexy about Stream.forEach(). Just use an enhanced for-loop, which is exactly the right tool for this job.
 
Rancher
Posts: 3571
39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, unfortunately checked exceptions don't play well with streams.  Yes, for forEach you can just switch to using a conventional for, but there are many other useful stream libraries where it's not as easy to switch.  

Then again, checked exceptions don't play well with a lot of things, and it's worth noting that of the various languages created after Java, while many took good ideas from Java, essentially none (that I know of) have imitated the concept of checked exceptions.

So, simple preferred solution: use Kotlin.

But since many of us are still stuck with Java (esp. at work) for various reasons, and many existing libraries use java.util.function.* interfaces, I recommend a suite of helper methods to smooth it out. You need new set of interfaces that mirror java.util.function.* and the like, but allow checked exceptions.  Then a bunch of methods to convert the checked versions to standard versions, all by catching the checked exception and rethrowing as an unchecked.  This will allow your forEach code to become something like:

A shortened version of Unchecked is:

The full version has converters for *all* the interfaces in java.util.function.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:But since many of us are still stuck with Java (esp. at work) for various reasons, and many existing libraries use java.util.function.* interfaces, I recommend a suite of helper methods to smooth it out. You need new set of interfaces that mirror java.util.function.* and the like, but allow checked exceptions.


I already wrote those for IOException and SQLException: https://github.com/robtimus/io-functions, https://github.com/robtimus/sql-functions. The latter includes an UncheckedSQLException which mirrors UncheckedIOException.

The call would become the following:
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic