Exception in the constructor of the injected object

835 views
Skip to first unread message

laolaoyu

unread,
Jul 19, 2011, 11:00:16 PM7/19/11
to google-guice
Hi,

I have a question regarding the usage of Guice constructor inject.
E.g.

class A {

public A () throws AException {...}

}

class B {

final private A a;

@Inject
public B (final A a) {
this.a = a;
}
}

As in the above case, the injected A's constructor may throw
exception. How should I handle this exception when using Guice?

Thanks.

Fred Faber

unread,
Jul 19, 2011, 11:09:36 PM7/19/11
to google...@googlegroups.com
You might benefit from CheckedProviders in this scenario:



--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


laolaoyu

unread,
Jul 19, 2011, 11:12:02 PM7/19/11
to google-guice
Modification on the above example:

public interface A {
}

public class AImpl implements A {

public AImpl () throws AException {...}

}

AImpl has been bound to A.

Fred Faber

unread,
Jul 19, 2011, 11:25:53 PM7/19/11
to google...@googlegroups.com
How do you want to handle the exception is the question?  You'd get a ProvisionException from Guice in this case.

laolaoyu

unread,
Jul 19, 2011, 11:35:39 PM7/19/11
to google-guice
At the moment, I am not sure how to handle the exception. But at least
I would like to be able to catch the exception so to have the option
of customized handling if I wish. With my example, I cannot catch it.
I will dig into the things you mentioned above so if it solves my
problem. You mentioned I will get a ProvisionException from Guice,
from it will I be able to know the actual exception that has been
thrown by AImpl's constructor?

Thanks for the help!
Reply all
Reply to author
Forward
0 new messages