How to handle exceptions in Collections2.transform()?

1,181 views
Skip to first unread message

tomhowe

unread,
Mar 4, 2011, 6:55:47 AM3/4/11
to guava-discuss
I am new to Guava (and relatively new to Java).. and I am trying to
use the Collections2.transform() method as follows:

List<Plan> listOfPlans = getListOfPlans();

List<Job> listOfJobs = Collections2.transform(listofPlans, new
Function<Plan, Job>() {

@Override
public Job apply(Plan plan) {
return translate(plan);
}
});

The problem I have is that translate() throws an Exception if it
fails. I would like this to bubble up but apply() does not let me
rethrow the exception.

I'm not sure the best approach to handle translate() failing?

Advice welcome.

Thanks, Tom

Robert Konigsberg

unread,
Mar 4, 2011, 7:24:11 AM3/4/11
to tomhowe, guava-discuss
try {
return translate(plan);
} catch(Exception e) {
throw new RuntimeException(e);
}

> --
> guava-...@googlegroups.com
> Project site: http://guava-libraries.googlecode.com
> This group: http://groups.google.com/group/guava-discuss
>
> This list is for general discussion.
> To report an issue: http://code.google.com/p/guava-libraries/issues/entry
> To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
>

--
Robert Konigsberg
konig...@gmail.com

Ditz

unread,
Mar 24, 2011, 8:09:14 AM3/24/11
to guava-discuss
Hi Tom,

if it is OK to abort the whole iteration process by a RuntimeException
you may choose the proposed solution.
You may decide to choose a special kind of Runtime Exception or design
your own Exception to be able
to handle (catch) it at a higher level within your embedding code.

Often, however, you don't want to interrupt the processing /
iteration.
Instead you wand to take some action and then continue processing the
other elements of the list.

Thus your catch block won't throw a Runtime Exception. Instead it will
take some action based upon
the Exception like logging some error message or to pop up a dialog to
ask the user what to do.
But finally you HAVE to produce any Job object to be feed to the
transformed list. You may choose
either to write NULL or some kind of NoJob object to notify the
receiver of the Job about the error situation.

You may even place the Exception you received into a special member of
your NoJob object to transport
the whole Exception to the receiver which may handle each exception
individually.

A more general solution would be to design a wrapper object similar to
Supplier<Job> like

interface JobContainer {


Ditz

unread,
Mar 24, 2011, 11:38:07 AM3/24/11
to guava-discuss
.. sorry, I accidentally pressed the commit button ....

with a helper Interface like:

interface Employer {
Job getJob() throws Exception;
}

to be able to transport a job and/or an exception simultaneously.

new Function<String, Employer>() {
public Employer apply(final Plan plan) {
return new Employer() {
public Job getJob() throws Exception {
return translate(plan);
}
};
}
};

PS: I made very bad experience of any "throws Exception" functions():

As RuntimeExceptions extends a normal Exeption you can't differ
between RuntimeExceptions
and checked Exceptions any more. This makes Exception handling very
difficult and confusing.
Either design a special kind of Exception (i.e. JobException) or use
something general like an IOException instead.

Reply all
Reply to author
Forward
0 new messages