How do I supply a custom exception to be thrown if the Preconditions reports a run time failure ?

1,238 views
Skip to first unread message

Rahul Agrawal

unread,
Apr 14, 2011, 7:41:24 AM4/14/11
to guava-discuss, rahula...@gmail.com
Example if I had something to be verified as :


if (count <= 0) {
throw new mycustomRuntimeException("must be positive: " +
count);
}

and I want to use the Preconditions, how do I achieve the same.

This may even lead to another question as to if it really addes value
to introduce a custom runtime exception in such a scenario.

-rahul

Robert Konigsberg

unread,
Apr 14, 2011, 7:59:13 AM4/14/11
to Rahul Agrawal, guava-discuss
In short, you don't. You can set up your own preconditions class that
has a similar structure to Preconditions if this is a common
occurrence.

I agree it addresses the value of a custom exception, but that's
something that can only be adequately answered in the context of your
project.

> --
> 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

Ricardo Redder

unread,
Apr 14, 2011, 4:15:35 PM4/14/11
to Robert Konigsberg, Rahul Agrawal, guava-discuss
I'm not sure if that helps you.... but can't you catch the Preconditions exception, and throw your custom exception, using the former as a your cause?

Something like:

try {
Preconditions.checkArgument(count > 0, "must be positive: %s", count);
} catch (IllegalArgumentException e) {
throw new MyCustomException(e);
}

ok... i agree it doesn't look nice, especially if you have multiple conditions to validate... but it might do the job.

Regards,
Redder

Robert Konigsberg

unread,
Apr 14, 2011, 4:18:02 PM4/14/11
to Ricardo Redder, Rahul Agrawal, guava-discuss
Given how little code is behind Preconditions, I'll just say that's
not worth it.

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

Ricardo Redder

unread,
Apr 14, 2011, 4:21:44 PM4/14/11
to Robert Konigsberg, Rahul Agrawal, guava-discuss
hehe.... well pointed
--
Redder

Benjamin Manes

unread,
Apr 14, 2011, 4:23:50 PM4/14/11
to Robert Konigsberg, Ricardo Redder, Rahul Agrawal, guava-discuss
If you wanted something reusable and custom, I imagine it would be easy to whip something using Hamcrest such as:

checkThat(count, is(greaterThan(0)), elseThrow(MyCustomException.class));

Robert Konigsberg

unread,
Apr 14, 2011, 4:46:18 PM4/14/11
to Benjamin Manes, Ricardo Redder, Rahul Agrawal, guava-discuss
Preconditions.throwIf(boolean b, RuntimeException e) {
if (!b) throw e;
}

throwIf(count > 0, new MyException());

Just kidding.

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

Sam Berlin

unread,
Apr 14, 2011, 4:50:20 PM4/14/11
to Robert Konigsberg, Benjamin Manes, Ricardo Redder, Rahul Agrawal, guava-discuss
On Thu, Apr 14, 2011 at 4:46 PM, Robert Konigsberg <konig...@gmail.com> wrote:
Preconditions.throwIf(boolean b, RuntimeException e) {
 if (!b) throw e;
}

throwIf(count > 0, new MyException());

While gross, that actually has the kinda nice property that the first line in the stack trace is the code you care about, as opposed to the Preconditions lines.

 sam

Colin Decker

unread,
Apr 14, 2011, 5:36:55 PM4/14/11
to guava-discuss
Another option:

public static void check(boolean b, Supplier<? extends RuntimeException> exceptionSupplier) {
  if (!b) {
    RuntimeException e = exceptionSupplier.get();
    e.fillInStackTrace();
    throw e;
  }
}

Could even make a version that took a Function<String, ? extends RuntimeException> to let Preconditions do its string building! (That might actually look half decent in Java 8 with constructor references.)

...seriously though, a simple "if (...) throw" seems best for less common exceptions.

-- 
Colin

morten hattesen

unread,
Apr 15, 2011, 3:36:52 AM4/15/11
to guava-discuss
If you were to choose this solution, you'd better prevent new
Exceptions from being constructed for each test by pre-constructing
the exception in a constant, wherever possible.

private static final RuntimException MY_EX = new MyException();

Preconditions.throwIf(boolean b, RuntimeException e) {
  if (!b) throw e;
}
...
throwIf(count > 0, MY_EX);


On 14 Apr., 22:50, Sam Berlin <sber...@gmail.com> wrote:
> On Thu, Apr 14, 2011 at 4:46 PM, Robert Konigsberg <konigsb...@gmail.com>wrote:
>
> > Preconditions.throwIf(boolean b, RuntimeException e) {
> >  if (!b) throw e;
> > }
>
> > throwIf(count > 0, new MyException());
>
> While gross, that actually has the kinda nice property that the first line
> in the stack trace is the code you care about, as opposed to the
> Preconditions lines.
>
>  sam
>
> > Just kidding.
>
> > On Thu, Apr 14, 2011 at 4:23 PM, Benjamin Manes <bma...@google.com> wrote:
> > > If you wanted something reusable and custom, I imagine it would be easy
> > to
> > > whip something using Hamcrest such as:
> > > checkThat(count, is(greaterThan(0)), elseThrow(MyCustomException.class));
>
> > > On Thu, Apr 14, 2011 at 1:18 PM, Robert Konigsberg <konigsb...@gmail.com
>
> > > wrote:
>
> > >> Given how little code is behind Preconditions, I'll just say that's
> > >> not worth it.
>
> > >> On Thu, Apr 14, 2011 at 4:15 PM, Ricardo Redder
> > >> <ricardo.red...@gmail.com> wrote:
> > >> > I'm not sure if that helps you.... but can't you catch the
> > Preconditions
> > >> > exception, and throw your custom exception, using the former as a your
> > >> > cause?
> > >> > Something like:
> > >> > try {
> > >> > Preconditions.checkArgument(count > 0, "must be positive: %s", count);
> > >> > } catch (IllegalArgumentException e) {
> > >> > throw new MyCustomException(e);
> > >> > }
> > >> > ok... i agree it doesn't look nice, especially if you have multiple
> > >> > conditions to validate... but it might do the job.
> > >> > Regards,
>
> > >> > On Thu, Apr 14, 2011 at 8:59 AM, Robert Konigsberg
> > >> > <konigsb...@gmail.com>
> > >> > wrote:
>
> > >> >> In short, you don't. You can set up your own preconditions class that
> > >> >> has a similar structure to Preconditions if this is a common
> > >> >> occurrence.
>
> > >> >> I agree it addresses the value of a custom exception, but that's
> > >> >> something that can only be adequately answered in the context of your
> > >> >> project.
>
> > >> >> On Thu, Apr 14, 2011 at 7:41 AM, Rahul Agrawal <
> > rahulagra...@gmail.com>
> > >> >> wrote:
> > >> >> > Example if I had something to be verified as :
>
> > >> >> >  if (count <= 0) {
> > >> >> >       throw new mycustomRuntimeException("must be positive: " +
> > >> >> > count);
> > >> >> >     }
>
> > >> >> > and I want to use the Preconditions, how do I achieve the same.
>
> > >> >> > This may even lead to another question as to if it really addes
> > value
> > >> >> > to introduce a custom runtime exception in such a scenario.
>
> > >> >> > -rahul
>
> > >> >> > --
> > >> >> > 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
> > >> >> konigsb...@gmail.com
>
> > >> >> --
> > >> >> 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")
>
> > >> > --
> > >> > Redder
>
> > >> --
> > >> Robert Konigsberg
> > >> konigsb...@gmail.com
>
> > >> --
> > >> 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
> > konigsb...@gmail.com

Robert Konigsberg

unread,
Apr 15, 2011, 5:50:46 AM4/15/11
to morten hattesen, guava-discuss
Yes, I was making a joke (see the "just kidding" at the bottom.)

> To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
>

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

Reply all
Reply to author
Forward
0 new messages