java – Why can't an Exception be thrown in an overridden (@Override) method?


Good evening everyone.

I came across an interesting feature of overridden methods – exceptions cannot be thrown in them, only processing within the method. But why? I would like to get an exhaustive answer to this question.


There are two types of exceptions in Java – checked and unchecked, you can read more, for example, in this article .

Checked exceptions are checked at the compilation stage of the application, and must be caught somewhere (catch), and the methods that throw such exceptions must have an exception type in their signature that can be thrown (for example, void method() throws Exception ). When overriding (@Override) a method that does not have throws in its signature, you cannot throw a checked exception, because the compiler cannot track it. Broken example:

class A {
    void foo() {}

class B extends A {
    void foo() throws Exception {}

A obj = new B();; // Компилятор рассматривает obj как A, в котором нет throws, и не знает о том, что он бросает checked-исключение

You can throw an unchecked exception based on RuntimeException or Error without adding throws to the overloaded method signature. Unless, of course, you don't want the checked exception.

Scroll to Top