By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

Viewing Exception Details

P: n/a
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.
Nov 19 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Ma*************@gmail.comwrote in message
news:08**********************************@35g2000p ry.googlegroups.com...
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.
Nov 19 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.