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 7891355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:35:53+00:00 2026-06-03T06:35:53+00:00

Is it better that a no-argument constructor throws an impossible exception or has an

  • 0

Is it better that a no-argument constructor throws an impossible exception or has an empty catch block? Let’s say I have a class like this.

public class Foo {
    private int num;
    private String numString;

    public Foo() throws NumberFormatException {
        setNum("1");
    }

    public void setNum(String s) throws NumberFormatException {
        // In reality, complex code goes here
        num = Integer.parseInt(s);
        numString = s;
    }
}

The compiler forces the constructor to either throw NumberFormatException (which would never happen) or have a try/catch block. However, would it be correct to have an empty catch block, which is usually frowned upon?

public Foo() {
    try {
        setNum("1");
    } 
    catch (NumberFormatException e) { }
}

Please note that Foo would be a library class and others would use it, so having a no-argument constructor throw an impossible exception would be confusing. Also note that the real exception is a custom exception, not NumberFormatException, which might make things even more confusing for the library user, who might feel like they have to read up on the custom exception when it is not necessary.

  • 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-03T06:35:54+00:00Added an answer on June 3, 2026 at 6:35 am

    NumberFormatException is a RuntimeException, so you don’t need to list it on your throws clause — just pretend it’s not there. That goes for both the method and the constructor.

    Any subclass of RuntimeException (including RuntimeException itself) is an “unchecked exception,” which means the compiler won’t force you to check for it in a try/catch clause. In contrast, any Exception which is not a subclass of RuntimeException is called, easily enough, a checked exception.

    If it were a checked exception (that is, not a subclass of RuntimeException), but you were sure that you could never trigger it, then it’d be safe to catch it. Rather than completely swallowing it, it’d be best to wrap it around an unchecked exception.

    try {
        thisCodeCanNeverThrowAnIOException();
    }
    catch (IOException e) {
        throw new AssertionError(e); // juuust in case!
    }
    

    Now your code compiles, doesn’t have a throws clause for an exception you never expect to be thrown, and yet will not hide a serious error if, by some bug or future change, that exception does end up getting thrown.

    (Note to those reading the comments: I originally had in the code example throw new RuntimeException(e), but Stephen C and Peter Lawrey pointed out that, in Java 1.4.2 and newer, AssertionError is better).

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

Sidebar

Related Questions

I have a class that has in its constructor some primitive type arguments, such
If a class has a single argument constructor my understanding is it is implicitly
I have two tables: sites has_many users users belongs_to sites Is it better that
I'm hearing that is better to have one connection open upon app start up
Suppose that you have to set 12 cookies, would it be better to store
I have a C++ class that contains a non-copyable handle. The class, however, must
Suppose you have a class with this constructor: public SomeObj(int x, int y) {
I have a class which takes two arguments on it's constructor, an int and
I have a lot of code in a big project that has two general
I know that is better to use Python to call Posix and Win API,

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.