Original Post
Hi. I'm trying to improve my coding skills by using exception handling on my game. But I got a few doubts that I would like to ask the wiser:
When I throw an exception in the constructor of an object, it destructs (by calling the destructor) the object which thrown the exception?
Let's say: I have an Image class within a Sprite class within the Game class. If the Image() constructor is called from the Sprite constructor, and the Image constructor throws an exception, the exception handling will destroy both the Image and then the Sprite?
Let's say that code above raises an exception (char*) because SDL_CreateRGBSurface returned an error. What is the expected behavior of m_pImage? It will be NULL? Or I will have to catch the exception in the Sprite constructor, free the memory of m_pImage and then rethrow it to the game class to catch it? Or the destructor will do the work for me?
This way I'm using exceptions is a good or a bad way of using it?
Thanks in advance.
When I throw an exception in the constructor of an object, it destructs (by calling the destructor) the object which thrown the exception?
Let's say: I have an Image class within a Sprite class within the Game class. If the Image() constructor is called from the Sprite constructor, and the Image constructor throws an exception, the exception handling will destroy both the Image and then the Sprite?
Image::Image(){ m_pSurface = SDL_CreateRGBSurface(...); if( m_pSurface == 0 ) throw( SDL_GetError() );}Sprite::Sprite(){ Image m_pImage = new Image;}Sprite::~Sprite(){ delete m_pImage;}Game::Start(){ try { Sprite pSprite = new Sprite; } catch(char* e) { std::cout << e << std::endl; }}Let's say that code above raises an exception (char*) because SDL_CreateRGBSurface returned an error. What is the expected behavior of m_pImage? It will be NULL? Or I will have to catch the exception in the Sprite constructor, free the memory of m_pImage and then rethrow it to the game class to catch it? Or the destructor will do the work for me?
This way I'm using exceptions is a good or a bad way of using it?
Thanks in advance.