Click here to Skip to main content
15,921,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the best method for receiving error information from a function/class or application and organizing the information?

I usually return/make public integer error values and have string error values which contain the exception information as public.
All the child function calls keep returning the integer value to the parent functions and this keeps on going until the main function get the error value.

Any advice, articles, books, discussions on how to do this?

Thanks
Posted

1 solution

Forget "error information" and "errors", embrace Structured Exception Handling. Forget collecting and organizing of such information, forget defensive programming in general, forget handling each and every exception case, let go. That all have gone to history.

It's hard to explain the concept in a one quick answer. Structured exception handling is the greatest invention; I value it more than whole OOP. Please see:
http://en.wikipedia.org/wiki/Structured_exception_handling[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms173160.aspx[^].

I provided a short explanation of the essence of things in my past answer: Does Exception in C# Constructor Cause Caller Assignment to Fail?[^].

Exception handling mechanism is a time machine.

—SA
 
Share this answer
 
Comments
nitrous_007 15-Apr-13 17:39pm    
I am not that much of an expert. So here is my attempt at digressing this information-
So are you saying that we should limit the number of try catches and put them in a higher level instead of putting the try catch in a very low level in the program?
What about other errors that are not exceptions?
Sergey Alexandrovich Kryukov 15-Apr-13 17:58pm    
Exactly, you caught one of the main points. What is this upper level? This is, first, a very top stack frame the stack of each thread. For a UI thread, the processing is special: in a main loop of the event-oriented UI. All UI library have a special even which you just handle, if you choose the Application option to use this mechanism (in my strong opinion, you always should). Is there something else? Generally, yes. Some exceptions can be handled on top of stack, but not on very top. Where? I call it "competency principle": in the method (stack frame) which is the most competent for handling certain type of exception. Could be more than on intermediate step: some "competency method" handles lower-level exception and "decide" to" 1) handle it completely and block further propagation; 2) handle partially and form an exception of higher level type, to be caught somewhere on top, 3) to re-throw it as is.

Run-time errors which are not exceptions? In modern programming, there are not such things. I can tell you what to do: if you face a legacy sub-system with "errors", create a new class derived from Exception, name it according to the sub-system. If its error can be distinctly classified into some different classes, consider further sub-classing to name them. Encapsulate some information sufficient to get all exception information. And throw those exceptions as early as possible. That its, escape "errors" into exception-handling domain.

—SA
nitrous_007 15-Apr-13 19:03pm    
If I create the a class derived from Exception and use it, what is the advantage over the following scenario-
Instead of creating another class, I just store the errorCode and error Strings in some variables and return gracefully from the program.
Is the Exception class just a good way of wrapping up the errorCode and errorString in a nice way? What other advantages would I get from an exception derived class?
Sergey Alexandrovich Kryukov 15-Apr-13 19:14pm    
It is like hell compared to heaven. You still did not get the idea. Never ever do what you just suggested. This is not just obvious, this is more than obvious.
I don't understand what to discuss here. Finally, learn exception handling well, to understand this; that's all you need.
—SA
nitrous_007 15-Apr-13 19:23pm    
Ok I will keep reading about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900