Click here to Skip to main content
15,905,616 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)


 
GeneralEnum for non-critical error Pin
Jcmorin19-Apr-07 4:18
Jcmorin19-Apr-07 4:18 
JokeTell the user... Pin
Jeremy Falcon19-Apr-07 0:08
professionalJeremy Falcon19-Apr-07 0:08 
GeneralRe: Tell the user... Pin
Paul Conrad22-Apr-07 10:08
professionalPaul Conrad22-Apr-07 10:08 
GeneralThe fifth option is doing well!! Pin
Nibu babu thomas18-Apr-07 2:37
Nibu babu thomas18-Apr-07 2:37 
JokeAutomatic firing of responsible developer Pin
Ashley van Gerven17-Apr-07 0:51
Ashley van Gerven17-Apr-07 0:51 
GeneralYeaaaaaaassss ! Pin
Kochise17-Apr-07 2:00
Kochise17-Apr-07 2:00 
GeneralRe: Yeaaaaaaassss ! Pin
StevenWalsh18-Apr-07 3:13
StevenWalsh18-Apr-07 3:13 
GeneralRe: Automatic firing of responsible developer Pin
Corinna John18-Apr-07 7:37
Corinna John18-Apr-07 7:37 
GeneralRe: Automatic firing of responsible developer Pin
Kochise18-Apr-07 21:36
Kochise18-Apr-07 21:36 
GeneralRe: Automatic firing of responsible developer Pin
ThatsAlok20-Apr-07 20:58
ThatsAlok20-Apr-07 20:58 
GeneralAnnas blog and Likely{T} Pin
peterchen16-Apr-07 23:46
peterchen16-Apr-07 23:46 
GeneralRe: Annas blog and Likely{T} Pin
Andre xxxxxxx20-May-07 3:25
Andre xxxxxxx20-May-07 3:25 
GeneralClaim it's Windows' fault ! Pin
Kochise16-Apr-07 21:52
Kochise16-Apr-07 21:52 
GeneralTry and Recover, Otherwise Die and Leave a Suicide Note Pin
Ri Qen-Sin16-Apr-07 8:33
Ri Qen-Sin16-Apr-07 8:33 
GeneralRe: Try and Recover, Otherwise Die and Leave a Suicide Note Pin
Almighty Bob16-Apr-07 11:43
Almighty Bob16-Apr-07 11:43 
GeneralCString Pin
bob1697216-Apr-07 5:27
bob1697216-Apr-07 5:27 
GeneralRe: CString Pin
Nemanja Trifunovic17-Apr-07 9:04
Nemanja Trifunovic17-Apr-07 9:04 
GeneralRe: CString Pin
bob1697217-Apr-07 10:26
bob1697217-Apr-07 10:26 
GeneralRe: CString Pin
StevenWalsh18-Apr-07 3:16
StevenWalsh18-Apr-07 3:16 
GeneralRe: CString Pin
ThatsAlok20-Apr-07 21:00
ThatsAlok20-Apr-07 21:00 
GeneralExceptions are good Pin
Gary Wheeler16-Apr-07 2:33
Gary Wheeler16-Apr-07 2:33 
GeneralRe: Exceptions are good Pin
bob1697216-Apr-07 5:22
bob1697216-Apr-07 5:22 
General...unless you are using C++ Pin
peterchen16-Apr-07 10:21
peterchen16-Apr-07 10:21 
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 
sorry for the delay - my internet connection went down yesterday..

First - this is not C++ specific - many comparisons of Error Code vs. Exceptions is biased by comparing non-RAII C code to RAII-based C++ code. using RAII simplifies the code - whether you exit with a return E_SomethingWentWrong;, or with a throw invalid_exception("Something went wrong");. Just check your local C++ book.

Second, C++ - specific: excpetion-safe code is hard to write, and almost impossible for templates. For a few starters (or maybe they are just arguments by authority...):

Writing correct Exception-based code is hard[^]
Another one[^]
A false sense of security[^] - the "famous stack invitation". Turns out you can't write an exception safe stack class template that offers T pop()


Third, "throwing from the deepest innards of that complex call tree" is certainly powerful and often welcome, but it is generally not useful. because getting an "array index out of bound" exception from the innards of RenderAllScenes() will neither help the user working arounf the problem, nor will it help you diagnose it. Even having a call stack here (like you get in C#) doesn't help much for nested loops. You need to catch, repackage and rethrow the exception at various levels to make it more useful than just "the grue is in there, lucky we made it out alive".
Also, these ugly complex algorithms often have a fallback mechanism what to do if they don't fin the file.

Fourth - C++ - specific again - in a normal (i.e. non-simplistic) application, exceptions don't propagate. They don't propagate across COM calls, across DLLs or Windows Messages (and don't even think of thread or process borders). And that's the places where I code in C++.

I am not trying to bash Exceptions, I am just trying to move them into perspective. They are good, because you can't really ignore error handling when calling code, but it makes it so easy to forget about fallback strategies and just throw up your arms in despair when you are writhing the code to be called.

just my thoughts.




We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist

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.