Enumerate Exceptions

G

Guitar Dude

I am trying to log exceptions. It seems like I have to write the code for
each member that I would like to log.
It would be nice to replace this with code to enumerate an exception, but it
seems that exceptions do not have .getEnumerator().

Any ideas?

I would like to replace this:
public void LogNullReferenceException(NullReferenceException
NullReferenceException1)

{

Console.WriteLine(NullReferenceException1.Data);

Console.WriteLine(NullReferenceException1.HelpLink);

Console.WriteLine(NullReferenceException1.InnerException);

Console.WriteLine(NullReferenceException1.Message);

Console.WriteLine(NullReferenceException1.Source);

Console.WriteLine(NullReferenceException1.StackTrace);

Console.WriteLine(NullReferenceException1.TargetSite);



}

with something like this psuedocode:

public void LogNullReferenceException(NullReferenceException
NullReferenceException1)

{

foreach (MemberInfo myMemberInfo in NullRefereneException1){

Console.WriteLine(myMemberInfo.Name + ":" + myMemberInfo.Value);

}



}
 
A

Alberto Poblacion

Guitar Dude said:
[...] this psuedocode:

public void LogNullReferenceException(NullReferenceException
NullReferenceException1)
{
foreach (MemberInfo myMemberInfo in NullRefereneException1){
Console.WriteLine(myMemberInfo.Name + ":" + myMemberInfo.Value);
}
}

using System.Reflection;
....
public void LogNullReferenceException(NullReferenceException
NullReferenceException1)
{
Type t = typeof(NullReferenceException);
foreach (PropertyInfo pi in t.GetProperties())
{
object value = pi.GetValue(NullReferenceException1, null);
Console.WriteLine(pi.Name + ":" + value.ToString());
}
}
 
B

Ben Voigt [C++ MVP]

Alberto said:
Guitar Dude said:
[...] this psuedocode:

public void LogNullReferenceException(NullReferenceException
NullReferenceException1)
{
foreach (MemberInfo myMemberInfo in NullRefereneException1){
Console.WriteLine(myMemberInfo.Name + ":" + myMemberInfo.Value);
}
}

using System.Reflection;
...
public void LogNullReferenceException(NullReferenceException
NullReferenceException1)
{
Type t = typeof(NullReferenceException);

Better:

Type t = NullReferenceException1.GetType();

Then you can change the parameter to System.Exception and it will work for
any sort of exception.

Also remember to walk the exception custom data dictionary (I forget what
it's called).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top