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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:39:37+00:00 2026-05-14T07:39:37+00:00

What is the best way to throw exception from the constructor initializer? For example:

  • 0

What is the best way to throw exception from the constructor initializer?

For example:

class C {
  T0 t0; // can be either valid or invalid, but does not throw directly
  T1 t1; // heavy object, do not construct if t0 is invalid,  by throwing before
  C(int n)
    : t0(n), // throw exception if t0(n) is not valid
      t1() {}
};

I thought maybe making wrapper, e.g. t0(throw_if_invalid(n)).

What is the practice to handle such cases?

  • 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-05-14T07:39:38+00:00Added an answer on May 14, 2026 at 7:39 am

    There are multiple ways of going about this, I think. From what I understand, n can only take on a specific range of numbers. For that, you might prevent the constructor from even being run:

    template <typename T, T Min, T Max>
    class ranged_type_c
    {
    public:
        typedef T value_type;
    
        ranged_type_c(const value_type& pX) :
        mX(pX)
        {
            check_value();
        }
    
        const value_type& get(void) const
        {
            return mX;
        }
    
        operator const value_type&(void) const
        {
            return get();
        }
    
        // non-const overloads would probably require a proxy
        // of some sort, to ensure values remain valid
    
    private:
        void check_value(void)
        {
            if (mX < Min || mX > Max)
                throw std::range_error("ranged value out of range");
        }
    
        value_type mX;
    };
    

    Could be more fleshed out, but that’s the idea. Now you can clamp the range:

    struct foo_c
    {
        foo_c(ranged_value_c<int, 0, 100> i) :
        x(i)
        {}
    
        int x;
    };
    

    If you pass a value that does not lie from 0-100, the above would throw.


    At runtime, I think your original idea was best:

    template <typename T>
    const T& check_range(const T& pX, const T& pMin, const T& pMax)
    {
        if (pX < pMin || pX > pMax)
            throw std::range_error("ranged value out of range");
    
        return pValue;
    }
    
    struct foo
    {
        foo(int i) :
        x(check_range(i, 0, 100))
        {}
    
        int x;
    }
    

    And that’s it. Same as above, but 0 and 100 can be replaced with a call to some function that returns the valid minimum and maximum.

    If you do end up using a function call to get valid ranges (recommended, to keep clutter to a minimum and organization higher), I’d add an overload:

    template <typename T>
    const T& check_range(const T& pX, const std::pair<T, T>& pRange)
    {
        return check_range(pX, pRange.first, pRange.second); // unpack
    }
    

    To allow stuff like this:

    std::pair<int, int> get_range(void)
    {
        // replace with some calculation
        return std::make_pair(0, 100);
    }
    
    struct foo
    {
        foo(int i) :
        x(check_range(i, get_range()))
        {}
    
        int x;
    }
    

    If I were to choose, I’d pick the runtime methods even if the range was compile-time. Even with low optimization the compiler will generate the same code, and it’s much less clumsy and arguably cleaner to read than the class version.

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

Sidebar

Related Questions

What's the best way to throw an exception in objective-c/cocoa?
The best way I can think of to ask this is by example... In
the best way to explain is with example so: this is the model public
What's the safest and best way to retrieve an unsigned long from a string
What's the best technique for exiting from a constructor on an error condition in
I'm wondering what the correct way is to pass on an exception from one
Consider some code that can throw a checked exception (an exception of type Exception
What's the best way of getting input from appwidget . I know that I
What is the best way to move database connective outside this class. So that
This question: Best way to return status flag and message from a method in

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.