Click here to Skip to main content
15,921,990 members

Survey Results

How do you return error conditions?   [Edit]

Survey period: 16 Apr 2007 to 22 Apr 2007

If a routine encounters an error do you hope the caller checks, or do you throw an exception to force the point? (inspired by Anna's blog)

Q1. For non-critical errors:

OptionVotes% 
Set a global error flag222.40
Return a reserved value (0 or -1 etc)27830.32
Return an encoded error value (eg HRESULT)14716.03
Throw an exception37340.68
Cover up the mess and don't say anything9710.58

View optional text answers (23 answers)
Q2. For critical errors that leave the app in an undefined state

OptionVotes% 
Set a global error flag444.80
Return a reserved value (0 or -1 etc)566.11
Return an encoded error value (eg HRESULT)626.76
Throw an exception69776.01
Cover up the mess and don't say anything505.45

View optional text answers (26 answers)


 
GeneralRe: ...unless you are using C++ Pin
Nemanja Trifunovic16-Apr-07 13:34
Nemanja Trifunovic16-Apr-07 13:34 
GeneralRe: ...unless you are using C++ Pin
peterchen16-Apr-07 21:45
peterchen16-Apr-07 21:45 
GeneralRe: ...unless you are using C++ Pin
Nemanja Trifunovic17-Apr-07 2:13
Nemanja Trifunovic17-Apr-07 2:13 
GeneralRe: ...unless you are using C++ Pin
Chris Losinger17-Apr-07 7:07
professionalChris Losinger17-Apr-07 7:07 
GeneralRe: ...unless you are using C++ Pin
Blake Miller17-Apr-07 9:09
Blake Miller17-Apr-07 9:09 
GeneralRe: ...unless you are using C++ Pin
Shog916-Apr-07 15:02
sitebuilderShog916-Apr-07 15:02 
GeneralRe: ...unless you are using C++ Pin
peterchen16-Apr-07 23:36
peterchen16-Apr-07 23:36 
GeneralRe: ...unless you are using C++ Pin
Shog917-Apr-07 5:37
sitebuilderShog917-Apr-07 5:37 
What happens if the copy construction of this object throws an exception? The pop operation fails because the object at the top of the stack cannot be copied (not because the stack is empty). Clearly, the caller does not receive a T object. But what should happen to the state of the stack object on which a pop operation fails in this way? A simple policy would be that if an operation on a stack throws an exception, the state of the stack is unchanged. A caller that removes the exception's cause can then repeat the pop operation, perhaps successfully.

I have yet to write a copy constructor that throws exceptions, and hope never to write one... however, there is the occasional case of the exception-throwing new during copy-construction...

Low-memory errors are a real problem to handle properly. Naive code may try to continue, and fail further down the line during another allocation, or due to not bothering to notice that the memory originally requested doesn't exist. IMHO, the best thing to do is fail quickly back to a stable point in the app, where perhaps the user can be notified and existing work preserved until additional resources are freed up.

Of course, none of that addresses the issue: we've still lost the top of the stack. Frankly, this doesn't concern me as much as it probably should - i never pass heap-allocating objects by value unless (like CString) they have a copy-on-write or (like auto_ptr) ownership transfer behavior such that they can effectively be treated as stack objects. And i consider stack overflow a catastrophic failure in any context. Poke tongue | ;-P

But i agree - it is a concern, and can't be ignored completely. Still, exceptions are an error-handling mechanism, and when left to that role i think they do fairly well. The important thing, always, is to handle errors only at the layer where they can be effectively dealt with. Sometimes, this means crashing and burning - i hate to say it, but it's true: some errors are such a severe indication of broken logic that it is dangerous to let the program continue. Exceptions work admirably for this as well... Wink | ;)



----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN

GeneralRe: ...unless you are using C++ Pin
peterchen17-Apr-07 7:32
peterchen17-Apr-07 7:32 
GeneralRe: ...unless you are using C++ Pin
Shog917-Apr-07 8:08
sitebuilderShog917-Apr-07 8:08 
GeneralRe: Exceptions are good [modified] Pin
User of Users Group17-Apr-07 13:06
User of Users Group17-Apr-07 13:06 
GeneralExceptions are evil Pin
Michael Dunn15-Apr-07 18:30
sitebuilderMichael Dunn15-Apr-07 18:30 
Generallongjmp() all the way! Pin
Shog915-Apr-07 19:12
sitebuilderShog915-Apr-07 19:12 
GeneralRe: longjmp() all the way! Pin
Gary Wheeler16-Apr-07 2:24
Gary Wheeler16-Apr-07 2:24 
GeneralRe: longjmp() all the way! Pin
John R. Shaw16-Apr-07 7:11
John R. Shaw16-Apr-07 7:11 
GeneralRe: longjmp() all the way! Pin
Gevorg17-Apr-07 5:37
Gevorg17-Apr-07 5:37 
GeneralRe: longjmp() all the way! Pin
Shog917-Apr-07 5:52
sitebuilderShog917-Apr-07 5:52 
GeneralRe: Exceptions are evil Pin
Nemanja Trifunovic16-Apr-07 2:01
Nemanja Trifunovic16-Apr-07 2:01 
GeneralEvent log Pin
Nisamudheen15-Apr-07 18:14
Nisamudheen15-Apr-07 18:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.