throws checked exception in RecordMapper:map method

9 views
Skip to first unread message

Denis Miorandi

unread,
Jul 21, 2016, 11:53:41 AM7/21/16
to jOOQ User Group
actually I use jooq mapper like this, but I don't know how to manage checked exception inside map method like throws.
I know this is not stricly a jooq stuff, but: any suggestions to do it in a smart way?

.fetchOne()
.map(new RecordMapper<Record, MyType>() {
@Override
        public MyType map(Record record) throws HERE??? {
                ....
                something that throw Exception 
                .....
        }
}


Actually I've seen something similar

Lukas Eder

unread,
Jul 21, 2016, 12:12:05 PM7/21/16
to jooq...@googlegroups.com
Very interesting question!

Given that you don't know what to do with the checked exception means that the checked exception probably shouldn't be checked in the first place (e.g. SQLException, IOException, etc.). The common approach here is to wrap it in a runtime exception (for SQLException, I suggest jOOQ's DataAccessException).

A bit less common is to "sneaky throw" the checked exception again:

You could also use jOOλ's Unchecked for this, possibly:

If you know you need to catch the specific checked exception somewhere outside of that mapper, then you'll have to be creative. But chances are high, you don't.

I hope this helps,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Denis Miorandi

unread,
Jul 21, 2016, 1:42:15 PM7/21/16
to jooq...@googlegroups.com
tks Lukas,
      I'm going to just catch Exception and throw a RunTimeException. This is enough.

Just cause I'm curious

Can Unchecked.consumer be used on .fetch().map(new RecordMapper.......) ???
Due to RecordMapper is a @FunctionalInterface i suppose it's possible but looking at code I didn't find the way.

Tks
Denis



--
You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/LJowoMTNEBc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Dott. Denis Miorandi
via dei Vignai 27
38060 - Nogaredo (TN)



Lukas Eder

unread,
Jul 22, 2016, 2:30:11 AM7/22/16
to jooq...@googlegroups.com
Hi Denis,

Sorry, I thought that there was an Unchecked.sneakyThrow() public method, but there isn't. It's package-private in SeqUtils. Will add that immediately:

The other methods can't really be used with RecordMapper. Well, they can, but they won't add a lot of value...

Lukas

Denis Miorandi

unread,
Jul 22, 2016, 2:35:13 AM7/22/16
to jooq...@googlegroups.com
cool. Tks Lukas.


Reply all
Reply to author
Forward
0 new messages