Viewing Exception Details

  • Thread starter Thread starter Magnus.Moraberg
  • Start date Start date
M

Magnus.Moraberg

Hi,

Is it a code practice for me to have writen the following Class? -

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace mine
{
static class ExceptionDetails
{
static public string Details(Exception exception)
{
string exceptionString = "";

try
{
int i = 0;
while (exception != null)
{
exceptionString += "*** Exception Level " + i + "
***\n";
exceptionString += "Message: " + exception.Message
+ "\n";
exceptionString += "Source: " + exception.Source +
"\n";
exceptionString += "Target Site: " +
exception.TargetSite + "\n";
exceptionString += "Stack Trace: " +
exception.StackTrace + "\n";
exceptionString += "Data: ";
foreach (DictionaryEntry keyValuePair in
exception.Data)
exceptionString += keyValuePair.Key.ToString()
+ ":" + keyValuePair.Value.ToString();
exceptionString += "\n***\n\n";

exception = exception.InnerException;

i++;
}
}
catch{}

return exceptionString;
}
}
}

I then use it as follows -

try
{
...
}
catch (Exception exception)
{
MessageBox.Show(ExceptionDetails.Details(exception), "Error.");
}

Thanks,

Barry.
 
Well, if you want to show the details, and this is what you want to see,
then by all means, use it. I'd probably move the call to MessageBox.Show
into the method as well (save some typing) and call it something else (like
DisplayDetails).

However, if you want to use this for other purposes (like logging to a
file) then it is just fine the way it is (although there are other logging
mechanisms out there).
 
Back
Top