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
> > >> >> To get help:
http://stackoverflow.com/questions/ask(use the tag
> > >> >> "guava")
>
> > >> > --
> > >> > Redder
>
> > >> --
> > >> Robert Konigsberg
> > >>
konigsb...@gmail.com
> > >> To get help:
http://stackoverflow.com/questions/ask(use the tag
> > "guava")
>
> > --
> > Robert Konigsberg
> >
konigsb...@gmail.com