How to report when an constructor fails???

Started by
20 comments, last by Spartacus 21 years, 8 months ago
Hey! How can I report to the calling function when the constructor of an object fails? Lets say I have a constructor like this: CObject::CObject(const CHAR srcFile[MAX_PATH]); and this constructor takes the path to a bmp file and loads the file. If the file does not exist, for instance, how can I notify the function constructing the object of this behaviour? Do I have to use exceptions or what?¿? Thanks! Spartacus

Real programmers don''t document, if it was hard to write it should be hard to understand

Real programmers don't document, if it was hard to write it should be hard to understand

Advertisement
Yes, you have to use an exception.

Another horrible way to do it is for the constructor to set a flag indicating it was constructed correctly and then check the flag each time an object is created.
I suppose exceptions could do it, or perhaps have a flag that gets set in the class that''ll tell the caller whether the object''s properly initialized or not. Still, I think the best way would to simply move any code that could fail right out of the constructor to somewhere else where it''ll be easier to test for failure.
There are three ways I can think of to do this:

1. as Melraidin said, move the initilisation out of the Constructor.

2. Melraidin''s other suggestion, set a flag and add an isValid() method.

3. Create a static method to construct the object, something like:

int clone(CObject* object, const CHAR srcFile[MAX_PATH]);

This can return an error code if anything goes wrong.

Exceptions would probably also work.

Pick whatever you like best!

Enigma
Throw an exception. That will ensure that your partially constructed object gets cleaned up correctly. i.e. the destructors for the members that have actually been constructed will be called.

The proper place for initialisation code _is_ the constructor. "Ressource acquisition is initialisation" does wonders.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Correct me if I''m wrong, but throwing exceptions from a constructor might (and in some cases definetly will) result in a memory leak.
It can, you have to code everything to be exception safe. Either use RAII religiously or use constructor-try blocks (which MSVC doesn''t support).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by kill
Correct me if I''m wrong, but throwing exceptions from a constructor might (and in some cases definetly will) result in a memory leak.

If throwing an exception in a constructor will result in a memory leak, your design is flaw.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
kill:
C++ spec says that if a constructor fails (throws an exception), the memory for the object being constructed is released. You wouldn't, for instance, write this code:

    Widget *pw;try{  pw = new Widget;}catch (...){  delete pw; // no need for this!  memory already released!}    

In fact, if you tried running this code as-is, you might get a memory access violation because if Widget's ctor fails, the assignment to pw never happens.

[edited by - Stoffel on August 13, 2002 6:24:48 PM]
quote:Original post by Magmai Kai Holmlor
or use constructor-try blocks (which MSVC doesn''t support).

Sure it does, at least MSVC.NET.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement