Hi,
I suggest checking out Herb Sutters Guru Of The Week website for some solid advice on when to use exceptions and the reasons why. Actually, read all of the GotW items coz all of the stuff there is well thought out by the guy who chairs the next C++ standard. [wink]
hth,
CipherCraft
How to make good use of exceptions?
Quote:Original post by The C modest godQuote:Original post by Wavarian
Just to echo what was said by everyone else - do not use exceptions to catch an error made on the programmer's behalf.
....
Why not to use an exception in that case?
Programmer error is a bad place to use exceptions because you need to handle exceptions somehow. It's better to use assertions to catch programmer error, with the exception of programmer error coming out of scripts, which generally then raises an exception in the scripting system, not a C++ exception.
Quote:Original post by Kitt3n
Even in this case I wouldn't throw an exception, but rather
fallback to a 'dummy' resource - for textures you could use
a big red cross.
/R
Well, I would fall back using an exception. [wink]
Throw an exception if the resource is missing, and then in the catch block, you insert the dummy resource instead. (And probably output an error message to a log file or something)
That way, you get to smoothly recover from errors, without having to surround everything with error code checking.
Quote:Original post by The C modest god
Why not to use an exception in that case?
What is so clear about allowing a bug to happen?
If you use an exception to catch this bug, then you can immediatly correct it.
I am sure a player would be happy to discover his character has negative intelligence or something like this.
Exception should be used to catch things you didnt intend that will happen.
You can quote this line.
If something happens in my program that I didnt intend it to happen, such as a red pixel in the bottom left part of the screen, I want to know about it. Even if the program would work with this bug.
An exceptional circumstance is one which you are unable to do anything about. If you have a bug in your code that allows a character to have a negative intelligence value then that's up to you to fix. If a required data file is missing, you can't do anything about that, it's out of your hands - so this would be a good reason to use an exception. Same goes for when CreateWindowEx() fails.
Quote:Original post by SiCraneQuote:Original post by The C modest godQuote:Original post by Wavarian
Just to echo what was said by everyone else - do not use exceptions to catch an error made on the programmer's behalf.
....
Why not to use an exception in that case?
Programmer error is a bad place to use exceptions because you need to handle exceptions somehow. It's better to use assertions to catch programmer error.
In most environments (not C++), though, assertions just throw exceptions anyway.
Quote:Original post by Arild FinesQuote:Original post by SiCraneQuote:Original post by The C modest godQuote:Original post by Wavarian
Just to echo what was said by everyone else - do not use exceptions to catch an error made on the programmer's behalf.
....
Why not to use an exception in that case?
Programmer error is a bad place to use exceptions because you need to handle exceptions somehow. It's better to use assertions to catch programmer error.
In most environments (not C++), though, assertions just throw exceptions anyway.
Perhaps, but labelling your assertions as assertions makes it easier to conditionally-compile them out, etc. Always prefer more specific/expressive constructs over general-purpose ones when possible (the same reason you use for instead of while where appropriate, or while instead of manually looping with a goto in a conditional).
Quote:Original post by WavarianQuote:Original post by The C modest god
Why not to use an exception in that case?
What is so clear about allowing a bug to happen?
If you use an exception to catch this bug, then you can immediatly correct it.
I am sure a player would be happy to discover his character has negative intelligence or something like this.
Exception should be used to catch things you didnt intend that will happen.
You can quote this line.
If something happens in my program that I didnt intend it to happen, such as a red pixel in the bottom left part of the screen, I want to know about it. Even if the program would work with this bug.
An exceptional circumstance is one which you are unable to do anything about. If you have a bug in your code that allows a character to have a negative intelligence value then that's up to you to fix. If a required data file is missing, you can't do anything about that, it's out of your hands - so this would be a good reason to use an exception. Same goes for when CreateWindowEx() fails.
Of course it is up to me to fix, but how can I fix it if I don't know when it happens because I didnt use an exception?
If something happens in your program that you didnt intend it to happen, even if now it is harmless, later it might cause a bug which you wont have any hell of idea why it happens.
Quote:Original post by The C modest god
Of course it is up to me to fix, but how can I fix it if I don't know when it happens because I didnt use an exception?
Then use an assertion instead.
SiCrane is right.
Do not mistake exceptions for use as a debugging tool, that's not their purpose.
Do not mistake exceptions for use as a debugging tool, that's not their purpose.
But the case above of a negative integer being passed where a positive integer is required is not neccessarily suitable for an assertation, as it is an invariant, and may need to be enforced even in release code.
So an exception could be more suitable in this case (think std::vector.at()).
So an exception could be more suitable in this case (think std::vector.at()).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement