Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a set of loadable data decoders for a specific type of data and a stream to read containing data. Now I want the program to select the correct decoder in a reliable way so I want to use a trial-and-error algorithm. It seems resonable to create a decoder and connect it to the stream using a constructor:

// C++ code
// codecs is an array of structs containing function pointers loaded from a dll
FileIn src("foo.wav");
for(size_t i=0; i<N_CODECS; ++i)
        WaveIn wavein(codecs[i],src);
        //Process data
    catch(...) //Swallow error. It's likly that another codec works. But if it were another error??
//no codec found (definitly an error)

Here, there is probably no true error before we have tried all codecs in the list. The reason to try-catch here is that it is not possible to get return code from the ctor. Would it be better to use zombie objects in this case?

EDIT: What the constructor does, is the following

WaveIn::WaveIn(Vtable& vt, FileIn& src):m_vt(vt)
    if(m_handle==NULL) //Resources allocated in DLL were released in this case.
         {throw "Bad file";}

The dtor:

share|improve this question
This is a religious argument and doesn't have a truly right answer. Both sides have valid points. I prefer to do minimal work in my constructors, especially if that work can fail. If it can fail, if possible, I wouldn't do it in the constructor. In your case, I would either try to find the correct codec prior to instantiating the object and pass it in through the constructor or have a separate method to find the correct codec within the WaveIn class. – Dunk Jan 30 '13 at 19:34

Many programmers see throwing exceptions to control flow as a code smell, because Exceptions (in Java, c and C++) are like non-local goto statements that are hard to understand and the chances for memory leaks are high.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.