Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:40:46+00:00 2026-06-15T23:40:46+00:00

Consider a Java class hierarchy such as: abstract class Exp { public void generate();

  • 0

Consider a Java class hierarchy such as:

abstract class Exp { public void generate(); }
class EffExp extends Exp {...}
clas PureExp extends Exp {...}
class NewExp extends EffExp {...}
etc, etc.

The implementation of generate inside NewExp was changed to throw an IOException.

public void generate() throws IOException {...}

The other implementations of generate don’t do any IO hence don’t need to throw it. The compiler complains and forces me to declare all methods in the class hierarchy to throw the exception. Is this really the only way forward? It seems rather intrusive.

I could of course catch the exception where it happens, inside NewExp, but that doesn’t really make sense. The exception should propagate to the top and stop the program execution.

The more general question here is: if you have a hierarchy of methods that override each other, do they all have to be declared to throw the same set of exceptions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:40:46+00:00Added an answer on June 15, 2026 at 11:40 pm

    Just remember One Rule: –

    • You cannot add more restriction to the method you override. The restrictions can be for e.g. : – adding an new Checked Exception, decreasing the method visibility.

    So, if you have to (no choice), make your overriding method throw a Checked Exception (Note that, you can add a unchecked Exception, though, but it wouldn’t make any sense), just add the exception in the throws clause of the method you are overriding.

    So, in simple words, the signature of your overridden method should match exactly with the one that overrides it.


    Now the simple reasoning behind this is – Polymorphism.

    In polymorphism, as you know that, you can have a super type reference point to a sub class object. So, you can have: –

    SupClass ref = new SubClass();
    ref.method1();
    

    Now, while checking for the existence of method1, compiler is only concerned about the reference type. So, it checks in SupClass, and allows the access accordingly.

    Now, imagine what happens, when at runtime, when ref is actually pointing to SubClass object, JVM finds that method1 throws a new exception, that was not checked by the compiler. It will crash. That is why it is not allowed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>
Consider the following Java class: public class Foo { public static void doStuff() {
Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
Consider this small test class: import java.util.List; public abstract class Test { // CAN
Consider the following Java exception classes: public class BarException extends RuntimeException { // [...]
Consider MyClass.java: public class MyClass { public void firstfunction(double fwd[]) { fwd[0] = 42;
Consider the situation where I have an abstract class in Java; public abstract class
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
Consider the following classes in Java class A { protected void methodA() { System.out.println(methodA()
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.