views:

116

answers:

3

I have to interface with some C code from C++ class constructor (Intel library)


  class A{
    A{
     x = ippiMalloc();
     if(x==NULL) ...
    }
  }

In the constructor malloc function (intel version) is used. If ippiMalloc function do not succeed what is the correct way to handle it. Throw exception?

+4  A: 

First of all, add parentheses to the constructor :) And yes, I would vote for throwing an exception, because the alternatives are not very pleasant, like setting some bool in class like object_state_is_not valid or something.

Armen Tsirunyan
+7  A: 

Yes, an exception would likely be the most appropriate way to handle the error here -- at least it would be the cleanest way. This is also what the "new" operator would do on failure.

If your app isn't exception aware than you have a problem. In which case a factory method on the class might make sense.

static A * create() { ... }

Here you can wrap this into a non-exception program, likely returning null if you can't create the object.

edA-qa mort-ora-y
Then again, if your app isn't return-null aware, you just introduced problems instead of removing them.
MSalters
If your app isn't exception aware it /must/ be checking return values. You really don't have another option: exceptions and/or return values.
edA-qa mort-ora-y
+1  A: 

Joining the previous answers - and here is a good explanation about why exceptions is the best way of error handling in constructors. (C++ FAQ Lite)

rursw1