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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:59:46+00:00 2026-06-14T03:59:46+00:00

Is the following definition bad style or plain wrong? That is, is it wrong

  • 0

Is the following definition bad style or plain wrong? That is, is it wrong to throw after having intialized a value that is considered incorrect?

myClass::myClass(int arg) : value(arg)
   {
    if (value < 0)
       throw (myException("Negative value not allowed!"));
   }

It seems preferable to use the intializer-list over assigning the value in the body, and from what I understand it does not matter if I throw after the value has been assigned or before.

  • 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-14T03:59:47+00:00Added an answer on June 14, 2026 at 3:59 am

    If value being >= 0 is a class invariant, then you really only have a couple of possibilities. One would be to write the ctor to accept a unsigned parameter, so a value <0 simply wasn’t possible. The other is to throw an exception.

    But yes, if the user passes a value from which you can’t create a valid object, then throwing an exception is entirely acceptable (and generally the right thing to do). The constructor’s responsibility is to create a valid object. If it’s passed a value from which it cannot create a valid object, than an exception is generally the correct response.

    As to when to throw the exception: at least in this case, it doesn’t really matter. The exception will roll-back creation of the current object, so it will never exist anyway. It could make a difference if it was a lot faster to test the value first, rather than copying the value then destroying it if it was invalid. With things like int that’s pretty much irrelevant. If you had, say, a large tree of some sort, and could tell whether it was valid by looking only at, say, the root node of the tree, it would probably be better to inspect that node first, and only copy the three if it was valid.

    The latter is obviously an optimization, but can be a big enough and simple enough one that it may be worthwhile even without profiling (arguably, it’s less optimization that just avoiding pessimization).

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

Sidebar

Related Questions

I know that having diamond inheritance is considered bad practice. However, I have 2
I have a WebForm that contains the following definition for the FCKeditor: <FCKeditorV2:FCKeditor ID=txtBody
Is the following definition of defaults bad practice? var defaults = { name:'', surname:'',
I have the following definition for an id field in an entity that is
In Domain Driven Design(DDD) a ValueObject has the following definition A Value Object is
I have the following definition for p: .contact p { margin: 5px 0 0
I have a function with the following definition: virtual bool Process(wtdaFileHandler &daHandler, wtdaGather &daGather);
I have jboss 7.1.1 with the following definition: <subsystem xmlns=urn:jboss:domain:logging:1.1> <console-handler name=CONSOLE> <level name=INFO/>
I have a struct Primitive, which has the following definition: typedef struct Primitive {
While reading a code I came across, the following definition and initialization of a

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.