Enumerate Exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R3VpdGFyIER1ZGU=?=

    Enumerate Exceptions

    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 LogNullReferenc eException(Null ReferenceExcept ion
    NullReferenceEx ception1)

    {

    Console.WriteLi ne(NullReferenc eException1.Dat a);

    Console.WriteLi ne(NullReferenc eException1.Hel pLink);

    Console.WriteLi ne(NullReferenc eException1.Inn erException);

    Console.WriteLi ne(NullReferenc eException1.Mes sage);

    Console.WriteLi ne(NullReferenc eException1.Sou rce);

    Console.WriteLi ne(NullReferenc eException1.Sta ckTrace);

    Console.WriteLi ne(NullReferenc eException1.Tar getSite);



    }

    with something like this psuedocode:

    public void LogNullReferenc eException(Null ReferenceExcept ion
    NullReferenceEx ception1)

    {

    foreach (MemberInfo myMemberInfo in NullRefereneExc eption1){

    Console.WriteLi ne(myMemberInfo .Name + ":" + myMemberInfo.Va lue);

    }



    }




  • Alberto Poblacion

    #2
    Re: Enumerate Exceptions

    "Guitar Dude" <GuitarDude@dis cussions.micros oft.comwrote in message
    news:B3897B02-A02F-488B-9A9E-D06FB31874EE@mi crosoft.com...
    [...] this psuedocode:
    >
    public void LogNullReferenc eException(Null ReferenceExcept ion
    NullReferenceEx ception1)
    {
    foreach (MemberInfo myMemberInfo in NullRefereneExc eption1){
    Console.WriteLi ne(myMemberInfo .Name + ":" + myMemberInfo.Va lue);
    }
    }
    using System.Reflecti on;
    ....
    public void LogNullReferenc eException(Null ReferenceExcept ion
    NullReferenceEx ception1)
    {
    Type t = typeof(NullRefe renceException) ;
    foreach (PropertyInfo pi in t.GetProperties ())
    {
    object value = pi.GetValue(Nul lReferenceExcep tion1, null);
    Console.WriteLi ne(pi.Name + ":" + value.ToString( ));
    }
    }

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: Enumerate Exceptions

      Alberto Poblacion wrote:
      "Guitar Dude" <GuitarDude@dis cussions.micros oft.comwrote in message
      news:B3897B02-A02F-488B-9A9E-D06FB31874EE@mi crosoft.com...
      >[...] this psuedocode:
      >>
      >public void LogNullReferenc eException(Null ReferenceExcept ion
      >NullReferenceE xception1)
      >{
      >foreach (MemberInfo myMemberInfo in NullRefereneExc eption1){
      >Console.WriteL ine(myMemberInf o.Name + ":" + myMemberInfo.Va lue);
      >}
      >}
      >
      using System.Reflecti on;
      ...
      public void LogNullReferenc eException(Null ReferenceExcept ion
      NullReferenceEx ception1)
      {
      Type t = typeof(NullRefe renceException) ;
      Better:

      Type t = NullReferenceEx ception1.GetTyp e();

      Then you can change the parameter to System.Exceptio n 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(Nul lReferenceExcep tion1, null);
      Console.WriteLi ne(pi.Name + ":" + value.ToString( ));
      }
      }

      Comment

      Working...