Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Exception.Data

Tags:

c#

exception

How have you used the Exception.Data property in C# projects that you've worked on?

I'd like answers that suggest a pattern, rather than those that are very specific to your app.

like image 723
Chris Avatar asked Sep 28 '08 01:09

Chris


People also ask

What is exception data?

If we find that something in your data is not correct and we can't start producing your order, we raise an “Exception”. Possible reasons are: the data is not complete – e.g. the drill file is missing. the data is ambiguous – e.g. top and bottom layers are not clearly defined.

How do you use exceptions?

Use exceptionsExceptions are thrown by code that encounters an error and caught by code that can correct the error. Exceptions can be thrown by the . NET runtime or by code in a program. Once an exception is thrown, it propagates up the call stack until a catch statement for the exception is found.

What is meant by exception handling?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.


2 Answers

The exception logger I use has been tweaked to write out all the items in the Data collection. Then for every exception we encounter that we cannot diagnose from the exception stack, we add in all the data in that function's scope, send out a new build, and wait for it to reoccur.

I guess we're optimists in that we don't put it in every function, but we are pessimists in that we don't take it out once we fix the issue.

like image 145
Austin Salonen Avatar answered Oct 05 '22 23:10

Austin Salonen


Since none of the answers include any code. Something that might useful as an addition to this question is how to actually look at the .Data dictionary. Since it is not a generic dictionary and only returns IDictionary

foreach(var kvp in exception.Data) the type of kvp will actually be object unhelpfully. However from the MSDN there's an easy way to iterate this dictionary:

foreach (DictionaryEntry de in e.Data)     Console.WriteLine("    Key: {0,-20}      Value: {1}",                               "'" + de.Key.ToString() + "'", de.Value); 

I don't really know what the format argument , -20 would mean, maybe Take(20)? Digressing... this code can be very helpful in a common error logger to unwind this data. A more complete usage would be similar to:

var messageBuilder = new StringBuilder();  do {                     foreach (DictionaryEntry kvp in exception.Data)         messageBuilder.AppendFormat("{0} : {1}\n", kvp.Key, kvp.Value);      messageBuilder.AppendLine(exception.Message);   } while ((exception = exception.InnerException) != null);  return messageBuilder.ToString(); 
like image 33
Chris Marisic Avatar answered Oct 05 '22 23:10

Chris Marisic