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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:50:15+00:00 2026-05-24T00:50:15+00:00

There is such class: class Circle{ int x; int y; int radius; public: Circle(int

  • 0

There is such class:

class Circle{
  int x;
  int y;
  int radius;
public:
  Circle(int x_, int y_, int radius_){
    x = x_;
    y = y_;
    if(radius < 0)
      signal error;
    radius = radius_;
  }
};

It is not possible to create circle with radius less than zero. What is a good method to signal error in constructor? I know that there are exceptions, but are there any other methods?

  • 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-24T00:50:17+00:00Added an answer on May 24, 2026 at 12:50 am

    A good method is not to allow the error to compile!

    Unfortunately, merely using unsigned will not prevent a negative value from being passed to the constructor – it will just be converted implicitly to an unsigned value, thus hiding the error (as Alf pointed out in a comment).

    The “proper” solution would be to write a custom nonnegative (or more generally, constrained_value) type to make the compiler catch this error – but for some projects this may be too much overhead, since it can easily lead to a proliferation of types. Since it improves (compile-time) type safety, I still think that this is fundamentally the right approach.

    A simple implementation of such a constrained type would look as follows:

    struct nonnegative {
        nonnegative() = default;
        template <typename T,
                  typename = typename std::enable_if<std::is_unsigned<T>::value>::type>
        nonnegative(T value) : value{value} {}
    
        operator unsigned () const { return value; }
    
    private:
        unsigned value;
    };
    

    See it in action

    This prevents construction from anything but an unsigned type. In other words, it simply disables the usual implicit, lossy conversion from signed to unsigned numbers.

    If the error cannot be caught at compile time (because the value is received from the user), something like exceptions are the next best solution in most cases (although an alternative is to use an option type or something similar).

    Often you would encapsulate this throwing of an exception in some kind of ASSERT macro. In any case, user input validation should not happen inside a class’ constructor but immediately after the value has been read from the outside world. In C++, the most common strategy is to rely on formatted input:

    unsigned value;
    if (! (std::cin >> value))
        // handle user input error, e.g. throw an exception.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is such class: #include <iostream> #include <cmath> class Element { private: int val;
I am trying to create a class as such: class CLASS { public: //stuff
Is there a good class repository like PEAR for PHP for other languages such
Is there any advantage over using a class over a struct in cases such
Is there such a thing as local, private, static and public variables in PHP?
File: Example1.java public class Example1 implements Runnable { public void run() { for(int i
Say I have the following POCO classes: public class Parent { public int ID
Is there such a thing as an x86 assembler that I can call through
Is there such a thing as having the most prefered design pattern for building
Is there such a thing as unit test generation? If so... ...does it work

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.