How does Guice handles exceptions during constructor injection

1,375 views
Skip to first unread message

Pedro Felix

unread,
Nov 5, 2009, 12:03:31 PM11/5/09
to google-guice
How does guice handles exceptions throwed by a constructor during
constructor injection?

Thanks
Pedro Felix

crazy...@gmail.com

unread,
Nov 5, 2009, 12:05:48 PM11/5/09
to google-guice
It wraps them and rethrows them (out through Provider.get(),
Injector.getInstance(), etc.).

Bob

Pedro Felix

unread,
Nov 5, 2009, 12:33:37 PM11/5/09
to google-guice
Thanks for the quick reply.
I have some more questions regarding this subject, however I didn't
found any good documentation regarding these issues. Do you know a
good one?
If the response is negative, here are my additional questions

1) Does this behavior apply to both checked and unchecked exceptions?
2) Does this behavior applies to exceptions throwed during property
injection?
3) If a dependency is optional, the occurrence of an exception when
creating the dependency object ends the "getInstance" or it this
dependency ignored?

Once again, thanks
Pedro Felix

On Nov 5, 5:05 pm, "crazybob...@gmail.com" <crazybob...@gmail.com>
wrote:

Bob Lee

unread,
Nov 5, 2009, 1:07:36 PM11/5/09
to google...@googlegroups.com
On Thu, Nov 5, 2009 at 9:33 AM, Pedro Felix <pmhs...@gmail.com> wrote:
1) Does this behavior apply to both checked and unchecked exceptions?

Yes.
 
2) Does this behavior applies to exceptions throwed during property
injection?

Yes.
 
3) If a dependency is optional, the occurrence of an exception when
creating the dependency object ends the "getInstance" or it this
dependency ignored?

The exception is thrown.

If you want to throw and catch an exception, I'd recommend implementing a custom factory that throws a checked exception. For example:

  interface MyProvider<T> {
    T get() throws MyException;
  }

Bob 

dg

unread,
Nov 6, 2009, 12:07:45 AM11/6/09
to google-guice
Also new-in-2.0 ThrowingProvider and ThrowingProviderBinder

On Nov 5, 10:07 am, Bob Lee <crazybob...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages