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

Enumerate Exceptions

P: n/a
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.InnerExc eption);

Console.WriteLine(NullReferenceException1.Message) ;

Console.WriteLine(NullReferenceException1.Source);

Console.WriteLine(NullReferenceException1.StackTra ce);

Console.WriteLine(NullReferenceException1.TargetSi te);

}

with something like this psuedocode:

public void LogNullReferenceException(NullReferenceException
NullReferenceException1)

{

foreach (MemberInfo myMemberInfo in NullRefereneException1){

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

}

}


Nov 19 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Guitar Dude" <Gu********@discussions.microsoft.comwrote in message
news:B3**********************************@microsof t.com...
[...] 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());
}
}

Nov 19 '08 #2

P: n/a
Alberto Poblacion wrote:
"Guitar Dude" <Gu********@discussions.microsoft.comwrote in message
news:B3**********************************@microsof t.com...
>[...] 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).
foreach (PropertyInfo pi in t.GetProperties())
{
object value = pi.GetValue(NullReferenceException1, null);
Console.WriteLine(pi.Name + ":" + value.ToString());
}
}

Nov 19 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.