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.)