Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dagger doesn't support constructors that throw #266

Open
diwakergupta opened this issue May 31, 2013 · 5 comments

Comments

@diwakergupta
Copy link

commented May 31, 2013

Dagger compiler doesn't complain, but compiling the generated class will fail with an error like:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-project: Compilation failure
[ERROR] target/generated-sources/annotations/MyClass$$InjectAdapter.java:[66,35] unreported exception FooException; must be caught or declared to be thrown

Seems like this behavior is by design, but it is not documented. So the immediate "fix" would be to document this constraint. Would be nice if the compiler can additionally detect and reject such constructors early.

@cgruber

This comment has been minimized.

Copy link
Collaborator

commented Jun 4, 2013

Well, let me open the question - should we handle these, and wrap, or should we reject them outright? I feel pretty scary about constructors that fail, and I'd say no to them, or wrap them in a @Provides method that handles the failure gracefully, perhaps returning a null-object pattern or some such.

In general, Providers that fail are degenerate from Dagger's point of view, and that includes naturally constructors we wrap in a provider automatically.

@swankjesse

This comment has been minimized.

Copy link
Member

commented Jun 5, 2013

The policy in Dagger 1.0 is to forbid checked exceptions. That's what we should do here, but with nicer error reporting rather than a compile failure in generated code.

We may want to permit these later, but possibly using a different mechanism. Guice has ThrowingProviders; we may want our own hooks that make checked exceptions work with dependency injection.

@BrantApps

This comment has been minimized.

Copy link

commented Jun 30, 2013

Hey guys, I'm building my app through the android-maven-plugin but when I include the dagger-compiler (all at v1.0.1) my build fails at the compile stage with;

<blah> ....com/oceanlife/task/DatabaseHelper$$InjectAdapter.java:[56,29] unreported exception
android.content.pm.PackageManager.NameNotFoundException; must be caught or declared to be thrown

Is this a manifestation of the same issue?

@cgruber

This comment has been minimized.

Copy link
Collaborator

commented Jul 1, 2013

Seems quite likely.

@JakeWharton

This comment has been minimized.

Copy link
Collaborator

commented Dec 26, 2015

Part of this issue is the fact that the generated "inject adapter" contains a constructor reference to types even if that type is never instantiated through Dagger. That issue is covered by #286, and once fixed will allow constructors that throw to be used as entry points for members-only injection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
5 participants
You can’t perform that action at this time.