Construction abort construction

I would like the constructor to interrupt the construction of the object whenever it encounters a certain error code (for example, if the following is encountered):

CudaObj::CudaObj(InsertionSim *theSim)
{
    // Setup
    if(cublasInit() == CUBLAS_STATUS_NOT_INITIALIZED) {
        printf("CUBLAS init error.\n");
        return -1;  // abort here rather than return a value
    }

        ...
}

      

What would be the easiest way to do this? Will there be exception handling?

+2


source to share


5 answers


I think the idiomatic way is to throw an exception from the constructor to emphasize that the object is not in a valid state.



+16




Exception handling will definitely be my choice, especially in the case shown above, which is indeed an exception from the correct program flow. Alternatively, you can let the constructor return and have an "IsInitilized ()" function, or some that will allow you / the user to verify that it was done appropriately. This does, however, impose additional burden on the user of your class, as it is said that you can go with exception handling as this is a well-accepted way of imposing this burden (they need to catch the exception).



+1


source


While I prefer exceptions, unless I have a VERY reasonable reason for exceptions, exceptions can have some disadvantages. This article lists some of the disadvantages and several alternatives to exception handling.
Even if you only use exceptions to handle errors, I still recommend reading them.

One more thing: IMO, you shouldn't limit your considerations to design errors. maintaining two different error handling infrastructures on the same system is tough and error prone. Try to select a system-wide error handling method.
while constructor error handling is an important factor in making this decision, it is not the only one.

+1


source


If you prefer not to use exceptions (although admittedly I do), you can make a static class method in which you ask, "Can I construct an object with these parameters?" and require this method to be called before you construct. So your constructor will become

CudaObj::CudaObj(InsertionSim *theSim)
{
    // Setup
    ASSERT(cublasInit() == CUBLAS_STATUS_NOT_INITIALIZED)    
        ...
}

      

And then you need

BOOL CudaObj::CanConstruct(InsertionSim *theSim)
{
    // Check if we can construct
    return TRUE; // Or FALSE
}

      

So your code will be

if (CudaObj::CanConstruct(pSim))
{
    pObj = new CudaObj(pSim);
}
else
{
    // Handle invalid parameter
    ...
}

      

You can also provide a convenience method for both (using OUT arguments for example).

0


source


Use an exception if you have no reason why you are not using exceptions in your program. The reason is that by using an exception, you prevent the constructor of the object from EVER, assuming the object is valid.

Another choice is to use the IsInitialized method (which will return false if the constructor fails) or a trivial (empty) constructor and a separate Initialize method that can return an error code. These strategies are good if you are avoiding exceptions and can be useful in situations where you are incorporating C ++ code into legacy C code.

0


source







All Articles