Yes - your Example
class is effectively declaring:
public Example() {
super();
}
That won't compile, because the super()
call is calling the A()
constructor which is declared to throw Exception
, which is a checked exception. That's just as much a mistake in a constructor as it is to call a method which declares that it throws a checked exception from within a method which neither catches the exception nor declares that it throws it itself.
So you need to declare the exception in an explicitly declared constructor in Example
.
public Example() throws Exception {
super(); // This is implicit; you can remove it if you want.
}
instead. Note that this is only relevant if the constructor throws a checked exception... unchecked exceptions don't need to be declared, so the "compiler-provided" default exception is fine.
Also note that you can't catch an exception thrown by a super-constructor.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…