Reputation: 1473
How do I add "throws" to an anonymous inner class's constructor?
class Foo {
public Foo() throws Exception {
}
}
Becasue this doesn't work
public static void main(String[] args) {
Foo x = new Foo() {
@Override
public Foo() throws Exception {
}
}
}
I'm trying to figure out where to put the "throws" to suppress the compiler warning. Is there a way to do this without using a try/catch block or making a separate class?
Upvotes: 2
Views: 8872
Reputation: 718798
I'm trying to figure out where to put the "throws" to suppress the compiler warning.
There is no way to do that with a throws
. The syntax of anonymous does not allow you to put a throws
clause on the (implicit) constructor. The (implicit) constructor implicitly throws all of the superclass constructor you are using. (And that is determined by the number and types of the parameters ...)
Aside: even if it did manage to do that, a throws
clause doesn't suppress the compilation error I suspect you are getting. Fundamentally, the superclass constructor declares that it may throw Exception
and adding a throws
couldn't change that fact ... without there being some way to handle the superclass constructor exception.
Is there a way to do this without using a try/catch block or making a separate class?
No, there isn't.
And I suspect that what you are trying to do is not even possible with a regular superclass / subclass. For example:
public class Foo {
public Foo(int i) throws Exception { }
}
public class Bar extends Foo {
public Bar(int i) {
super(i);
}
}
There is no way to get that to compile because there is no way for the Bar
constructor to either suppress or handle the exception that the Foo
constructor declares that it throws.
(And there is a sound reason for this. If Foo(42)
did throw an exception, then when the exception reached the Bar
constructor it would have to deal with the state of a partially constructed Foo
. This is not practical, and even if it was, it would be a bad idea.)
Upvotes: 0
Reputation: 10298
If you really need to enforce the consistency of instances of an anonymous class, you can create them in a factory method that throws an exception if the arguments are invalid. This only works for anonymous instances of an interface, since the anonymous class cannot be the return type for a method.
A completely pointless example:
public class AnonymousException {
public static void main(String args) {
newRunnable(42).run();
}
private static Runnable newRunnable(final int x) {
if(x < 0) throw new IllegalArgumentException();
return new Runnable() {
@Override
public void run() {
System.out.println("x=" + x);
}
};
}
}
Upvotes: 0
Reputation: 25419
I'm not sure what you are trying to accomplish. Note that an anonymous class never has an explicitly declared constructor. This is quite understandable since if it had one, it would need to have a name and hence no longer be anonymous.
If you want to have an anonymous subclass of a class where the constructor might throw, this is not a problem.
class Foo {
Foo() throws Exception {
}
}
void demo() throws Exception {
Foo foo = new Foo() { // might throw
};
}
Likewise, if you want to initialize the anonymous class with something that might throw, you can do it as well. Remember that definitions of anonymous classes are expressions so they can throw as long as they are surrounded by a try
-catch
block or the containing method declares the exception to be thrown.
class Foo {
}
class Bar {
Bar() throws Exception {
}
}
void demo() throws Exception {
Foo foo = new Foo() {
Bar bar = new Bar(); // might throw
};
}
Upvotes: 4
Reputation: 8571
You cannot declare a constructor on an anonymous class.
From the Java language specification, section 15.9.5.1, "Anonymous Constructors": An anonymous class cannot have an explicitly declared constructor.
If you need a new constructor, make the class an inner class (but not anonymous) or even a top level class.
Upvotes: 2