The following code snippet is from the book OCA/OCP Java SE 7 Programmer 1 & 2 Study Guide by Kathy Sierra and Bert Bates.
public void rethrow() throws SQLException, IOException {
try {
couldThrowAnException();
} catch (Exception e) { // watch out: this isn't really
// catching all exception sublclasses
log(e);
throw e; // note: won't compile in Java 6
}
}
The text on page 393 states that "You may have noticed that couldThrowAnException() doesnt actually throw an exception. The compiler doesn't know this. The method signature is key to the compiler. It can't assume that no exception gets thrown, as a subclass could override the method and throw an exception."
Wouldn't that be incorrect or at least misleading cause a subclass cannot declare new or broader checked exceptions. Or am I missing something?
If so is the author referring to unchecked exceptions here? But then the compiler does not care about the unchecked exceptions one way or the other. I am missing the point being made here. Could someone please help me understand. Thanks.