Sign in to follow this  

short questions on exception handling

This topic is 402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I have a few questions on exception handling in C++, can someone shed some light on this?

 

1. When I use a 'throw', in debug mode a nice pop-up shows up, helping me in the direction to solve my underlying problem.

Now when I make a release build and run it outside VS, and this throw is done, the result is 'The pogram has stopped working!'.

Can I assume that independent of the details of the throw (runtime_error, memory allocation etc.) the program will stop working (release mode)?

 

If so, I know I could decide to add some messagebox or logging line that tells what happened and why the program stopped.

 

2. Is there any harm/ advice against using 'throw' without a try/catch blok?

With the assumption that it regards situations that are predictable, like if somePtr == nullptr etc.

Some testing showed me that it works perfectly fine, assuming you know that it could occur in some condition.

 

3. Will a 'throw' only statement in a constructor, make the program call the destructor of this class?

(besides the individual constructors of the member vars in the class).

If not, I should make sure that all class members are 'RAII proof' / have a good destruction method on their own, for pointers CComPtr (smart pointer) etc.

 

Note; it's ofcourse situational, because I could also have some function return a bool, in this case false, if the input pointer == nullptr.

It's just that I want to know if both options are considered 'OK/ valid'. In exceptional situations I might need just a throw in a constructor (better not, and have some other member function do the tricky stuff, but this is not always possible. This is a balance / design decision; how much initializating does my object need to be in a 'valid state'.

 

Any input is appreciated.

Share this post


Link to post
Share on other sites

This topic is 402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this