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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:10:38+00:00 2026-05-17T03:10:38+00:00

I have a class whose methods require that a certain class field exists correctly.

  • 0

I have a class whose methods require that a certain class field exists correctly. That class field is set in the constructor and it’s read from a config file, and it may or may not get the correct data from that config file. If the data is incorrect, it will have the wrong data in the class field and the class method will throw an exception.

If that happens, what I want to do is run the method again but with a different call to the class constructor. Is this something that is reasonably handled in a try:catch? Because the method MAY throw the same exception even with the correct class field. So what I want is to have the first time the method is called, the exception is caught and then the method run again. But on the 2nd run if the exception is thrown I want it to propagate. So:


try:
    MyClass().method()
except MyException:
    MyClass(True).method()

Is there an obvious flaw to this? Or a better way to do this without using counters, or flags, or other ugly helper objects?

  • 1 1 Answer
  • 1 View
  • 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-05-17T03:10:39+00:00Added an answer on May 17, 2026 at 3:10 am

    I’m a bit surprised that you don’t want the MyClass instance to stick around for later use, but, given that this is your intention, your code is correct and concise — it does what you state you want without any “obvious flaw”. I’m not sure which objects you think ugly and which ones you think pretty, but without introducing some “helper objects” I can’t even think of another way to achieve your stated requirements!-)

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

Sidebar

Related Questions

Suppose I have a file X.h which defines a class X, whose methods are
I have a class whose constructor takes a const reference to a string. This
I have a class whose functionality I'd like to depend on a set of
I have a base class that I inherit from that has two zero to
We have a class whose semantic behaviour is like the following :- struct Sample
Why does the following program crash? I have a base class whose destructor is
I have a CurrentUser class with a static property whose value was stored in
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have a class header file called Grid.h that contains the following 2 private

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.