This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Thanks, I understood that Unchecked Exceptions do not need to be thrown.
Now, is it possible to throw any Checked Exception in an over-ridden method, that is not mentioned to be thrown in the source method???
Not checked exceptions, no (unless they're a subclass of something that is declared in the parent).
The reason is that it would violate the Liskov Substitution Principle. Consider this example:
If this was allowed, the compiler would allow the try/catch block, because it would treat the variable as a SuperClass object. It knows the only checked exceptions that can throw are IOExceptions, and these are handled. But obj is actually a SubClass object, and so line 12 might actually throw a SQLException. This would mean the compiler has allowed the possibility of a checked exception not being handled - which defeats the whole point of them. The sensible way to prevent this is to not allow the method to be overridden like this.