Home > Java > javaTutorial > Can a Subclass Method Throw Broader Checked Exceptions Than Its Superclass Method?

Can a Subclass Method Throw Broader Checked Exceptions Than Its Superclass Method?

Linda Hamilton
Release: 2024-11-29 09:57:11
Original
958 people have browsed it

Can a Subclass Method Throw Broader Checked Exceptions Than Its Superclass Method?

Understanding Overriding Method Exception Declarations

In Java, when overriding a method in a subclass, the overriding method must adhere to certain rules regarding thrown exceptions. Specifically, the overriding method cannot throw checked exceptions that are new or broader than those declared by the overridden method.

For instance, if a base class method declares to throw a FileNotFoundException, the overriding method in a subclass cannot throw a SQLException, Exception, or any other non-runtime exception that is not a subclass of FileNotFoundException.

Reasoning Behind the Restriction

This restriction stems from the principle of polymorphism and the Liskov Substitution Principle (LSP). LSP states that a subclass should be substitutable for its base class in all circumstances, such that the program behavior remains correct.

If the overriding method were allowed to throw broader exceptions, it would violate LSP. For example, if the base class method foo() declares to throw an IOException, and the overriding method declares to throw a SQLException, the compiler would no longer force the caller to handle IOException.

However, when a subclass is treated as its base class, the caller may not expect or be prepared to handle broader exceptions thrown by the overriding method. This would lead to runtime exceptions and unpredictable behavior.

Example

Consider the following code:

class A {
   public void foo() throws IOException {..}
}

class B extends A {
   @Override
   public void foo() throws SocketException {..} // allowed

   @Override
   public void foo() throws SQLException {..} // NOT allowed
}
Copy after login

In this example, the overriding method in class B can throw a SocketException because it is a subclass of IOException. However, it cannot throw a SQLException because SQLException is not a subclass of IOException.

Unchecked Exceptions

Unchecked exceptions are not subject to this restriction. They can be thrown by overriding methods regardless of whether they are declared in the overridden method's throw clause.

The above is the detailed content of Can a Subclass Method Throw Broader Checked Exceptions Than Its Superclass Method?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template