Overridden methods cannot throw exceptions Java [duplicate]

Overridden methods can throw Exceptions, so long as the method being overridden also throws the same Exceptions. You can’t introduce new Exceptions.

So why can’t you introduce a new Exception?

One of the central concepts of OOP is using abstract types, and that all subtypes may be treated as the abstract type. See Liskov Substitution Principle

The reason you can’t introduce broader behaviour is that if the method from the abstract type (super class or interface) doesn’t throw an Exception and you refer to your object as that type, you’d get unexpected behaviour:

Alpha alpha = new Beta();
// At this point, the compiler knows only that we have an Alpha
alpha.myMethod();

If Alpha’s myMethod() doesn’t throw an Exception, but Beta’s does, we could get an unexpected Exception in the above code.

Leave a Comment