Throwing Exception in constructor?

Started by
7 comments, last by Zakwayda 13 years ago
I have the following questions about throwing exceptions while in an objects constructor.
1.) If you throw an exception in a constructor...does the destructor for that object get called?
2.) If your throw an exception in a constructor, does an opened handle(on windows) or file pointer(in C/C++) remain open and memory leaked until your manually release the resource?
Advertisement
  1. If an exception is thrown inside the constructor for an object, then that object's destructor is NOT called. The destructors of any member objects it contains, and the destructors of any classes it inherits from however will be.
  2. Exception handling won't do anything about any open files or allocated objects unless they're managed in some objects destructor (like std::filebuf or std::auto_ptr) or explicitly dealt with in a try/catch block. As such, the answer is probably yes, you'll create a leak if an exception is thrown.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
awesome information, im just curious can you use auto_ptr with handles in windows?
No, std::auto_ptr cannot be used with a Windows HANDLE. Since we're on the subject, it also can't be used for arrays.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
im guessing i would need to create my own RAII object for this then?

im guessing i would need to create my own RAII object for this then?


Or use [font="Courier New"]std::shared_ptr [/font]with a destructor argument.

Stephen M. Webb
Professional Free Software Developer

Or the ATL CHandle class.
I believe shared_ptr is part of C++0x, which may not be something one would want to mess with yet.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

I believe shared_ptr is part of C++0x, which may not be something one would want to mess with yet.

There's always the Boost version though.

This topic is closed to new replies.

Advertisement