Clarke
Clarke

Reputation: 305

Why doesn't Java support adding `throws` to the method signature?

This is a question that is being around in my head for days.

When it comes to inheritance and polymorphism, Java support covariant return types through the bridge method concept.

But why doesn't they implement a concept like bridge method to the methods that don't have throws clause in the superclass, but has a throws clause when it is overridden in the subclass. Or can't they add the throws to the method signature to overcome this problem.

From this post,

I get the idea that when override a method from a superclass, the checked exceptions that the subclass method declares cannot be more general than those of the superclass method.

But why can't we override a method that doesn't have a thorws in the superclass, but to have it in the subclass.

Why my suggestions can't be applied to the Java mechanism.

Can someone explain it to me

Upvotes: -1

Views: 383

Answers (1)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

The throws clause is ignored by the JVM verifier, so there is no need for a bridge or other synthetic method.

The compiler enforces the language constraint that the throws specification is no broader than the overridden method. This is the same covariant behaviour as for return types (since 1.5). Answers to the linked question demonstrate why this is the case.

Upvotes: 0

Related Questions