Enumerate Exceptions

  • Thread starter Thread starter Guitar Dude
  • Start date Start date
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);

}



}
 
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());
}
}
 
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).
 
Back
Top