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

  • Home
  • SEARCH
  • 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 8739847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:01:24+00:00 2026-06-13T11:01:24+00:00

I am beginner with java, please forgive me if I do make any error

  • 0

I am beginner with java, please forgive me if I do make any error and suggestion are welcome I would be happy to listen about the errors I made. Thanks In advance for reading and replying to my question.

I get the below code from some book

In the code the new exception is thrown but still the code compiles and one more code similar to that I am pasting but could not understand the different behavior

The first code prints X but according to me the overridden method foo() should not throw new exception. Secondly while calling the method foo, try–catch should be used if a throws clause is there:

class X { public void foo() { System.out.print("X "); } }    

public class SubB extends X {

    public void foo() throws RuntimeException {
        super.foo();
        if (true) throw new RuntimeException();
        System.out.print("B ");
    }

    public static void main(String[] args) {
        new SubB().foo();
    }
}

The second code is similar to above but in this we need to use try–catch while calling the method:

class A {
    void foo() throws Exception { throw new Exception(); }
}

class SubB2 extends A {
    void foo() { System.out.println("B "); }
}

class Tester {
    public static void main(String[] args) {
        A a = new SubB2();
        a.foo();
    }
}

In this exception is extended in the parent class foo method but in the above code the exception is in the subclass’s foo method.

  • 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-13T11:01:25+00:00Added an answer on June 13, 2026 at 11:01 am

    In the first code, you have declared to throw a RuntimeException, which is an Unchecked Exception and does not need to be handled. And hence for this reason, you can also add it in the throws clause of the overriden method, even though it is not in the parent class method.

    However, in the 2nd code, you have declared to throw Exception which is at the top level in the Exception hierarchy, and is Checked by the Compiler and you need to declare in the parent class method throws clause if you are throwing it. Also, you need to handle it too, while invoking the method.

    Though you cannot increase the restriction in the overriden method in base class, in the sense, you cannot add an extra Checked Exception in the overriden method, however, if you add an UncheckedException, it is allowed.

    You can go through the following links to know more about Exception Handling: –

    • http://docs.oracle.com/javase/tutorial/essential/exceptions/
    • http://tutorials.jenkov.com/java-exception-handling/index.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a Java-beginner, so please bear with this one. I have a class: class
Iam a Java beginner and i would like to ask whats the pros and
I am a Java beginner and I would appreciate it if someone here can
I'm still a beginner in java. I dont have any idea on how to
I am a beginner in java .So please let me know how can i
i'm beginner and i found an old thread about lastmodified files in java. What
I am currently switching languages from Java(beginner) to c++ and would like to replicate
I am a java beginner and I want to make a simple text editor
Beginner testng always add the parameter error Master please help look at Thank you!!
I'll preface my question by saying that I'm a beginner Java EE developer. I

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.