0

Suppose I have a static method of my class that returns an object of the same type of my class. To create the object for example this method have to parse a string:

class C
{
public:

   static C get_obj(const std::string& str)
   {
      C obj;
      // Parse the string and set obj properties
      return obj;
   }
};

If, when I parse the string, I get an error and the object can't be constructed as a valid object, have I to throw an exception or what else?

Nick
  • 10,309
  • 21
  • 97
  • 201

4 Answers4

3

I'd say you should throw an exception. This way you notify the client that the obj could not be obtained, and force him to deal with this.

If not important (not critical), you could return a special C that would act as a sentinel value indicating that something went wrong. The client will choose whether to do something about it or not.

I'd go with the exception. The second approach is not recommended.

Tom
  • 43,810
  • 29
  • 138
  • 169
3

Given that there is a possibility of failure in get_obj the failure must be reported back to the caller in some manner. This is typically either done by

  • Throwing an exception
  • Communicating the failure in the output of the method

In this particular case the only output of the method is a C instance. Given that throwing an exception is probably the best option for a method of this signature. The only other choice is to embed the success / failure inside the C object which you almost certainly don't want to do.

Another way to approach this problem is the try_parse pattern. Let a bool return indicate success / failure and return the constructed object on success through a reference parameter

bool try_parse(const std::string& str, C& obj) {
  if (string is valid) { 
    obj = C(...);
    return true;
  }

  return false;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • And how exactly would you call your `try_parse` function? With a default constructed `C` object? – Praetorian Jul 06 '12 at 19:32
  • @Prætorian yes. if you notice from the original question though that appears to be a supported scenario for `C` as the author does it in the implementation of `get_obj`. Barring a default constructor i would use a `shared_ptr& or similar mechanism – JaredPar Jul 06 '12 at 19:33
  • It works that way, but IMO the call looks ugly. `C foo; if( !try_parse( "blah", foo ) ) { /* Error, do something */ }`. Might as well be `C foo( "blah" );` surrounded by a `try-catch`. There isn't really a clean way to implement `try_parse` unless you're dynamically allocating the object. – Praetorian Jul 06 '12 at 19:39
  • 2
    @Prætorian not much of a fan of the code either. But in the cases where `C` has a valid default construction I prefer it over dynamic allocation – JaredPar Jul 06 '12 at 19:45
0

Yes it is perfectly valid to throw an exception. Its the same reason when constructing an object, if you cannot proceed with the construction of an object you have very little choice but to throw an exception.

JonH
  • 32,732
  • 12
  • 87
  • 145
-1

Yes, you do need to throw an exception.

class C
{
public:

   static C get_obj(const std::string& str)
   {
      try
      {
          C obj;
          // Parse the string and set obj properties
          return obj;
      }
      catch (int x)
      {
        cout "blahblah";
      }
   }
};

If the object cannot be constructed you risk a 0 variable, which can cause a lot of trouble further on

Sibin Grasic
  • 714
  • 9
  • 27
  • exception not caught as a reference ? Not sure Meyers will agree: http://stackoverflow.com/questions/2522299/c-catch-blocks-catch-exception-by-value-or-reference – Tom Jul 06 '12 at 19:25
  • 4
    What's the point of throwing an exception and catching it immediately at the throw site? Edit: -1 after reading the last sentence, missed it earlier. What the heck is a *0 variable*? – Praetorian Jul 06 '12 at 19:27
  • 1
    @Tom who the hell is Meyers?! – Nick Jul 06 '12 at 19:27
  • @Nick he wrote Effective C++ and a few other books, he knows C++ very well and his writing style is very effective. – JonH Jul 06 '12 at 19:28
  • @Nick This guy : http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876/ref=sr_1_1?ie=UTF8&qid=1341602856&sr=8-1&keywords=effective+c%2B%2B http://www.amazon.com/More-Effective-Improve-Programs-Designs/dp/020163371X/ref=sr_1_3?ie=UTF8&qid=1341602856&sr=8-3&keywords=effective+c%2B%2B – Tom Jul 06 '12 at 19:28
  • @Nick: https://www.google.com/search?q=Scott+Meyers&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – Martin York Jul 06 '12 at 22:44