Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo

  Exception properties (Monday, December 13, 2004)




Found the following interesting discussion in the Newsgroups:

Exception properties
by:Ray Cassick \(Home\)

I am trying to create a generic logging component that will accept an
exception object and create a string containing all the properties and their
values available for a specific exception.

My intent here is try to create a generic function that will be able to:

- Take a an object of type Exception...
- Iterate through all the properties the object has...
- Add the property names to a stringbuilder...
- Call each of the properties and get the values...
- Add each of the values to the string builder.

I can get all the basic properties that System.Exception provides (Message,
Source, Inner Exception, Target Site, Stack Trace, etc...) but I would also
like to be bale to get any customer properties provided by the more specific
exceptions and also print them out.

Can anyone point me to a piece of code that will help? I would also like to
be able to print the specifics on custom exceptions that are not part of the
framework but I am thinking that I am not going to be able to get much
deeper than the standard properties provided by the base exception the
custom exception inherits from.

Any ideas out there?



 Reply:
by:Chad Z. Hower aka Kudzu

 
Look in MSDN for "reflection".
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/


 Reply:
by:Jay B. Harlow [MVP - Outlook]

 Ray,
I would expect custom Exceptions to override the Exception.ToString method
to include this information in a readable & sensible format for you.

All the exception that I have used from the Framework do this for you, I
would expect any one implementing their own exceptions to also do the same!
Either by overriding Exception.ToString itself or overriding
Exception.Message (which ever made the most sense for the use of the custom
Exception).

Hope this helps
Jay


 Reply:
by:Ray Cassick \(Home\)

 Yeah, I know about that property. I was just trying to get something wrapped
up that would be a bit more standardized and not depend on the person that
designed the exception class.


 Reply:
by:Jay B. Harlow [MVP - Outlook]

 Ray,
As Chad stated, you can use Reflection to do what you want.
IMHO doing the exception correctly is the "standardized" part! The person
designing the exception would/should design the exception per standards!

Also won't you be logging duplicate information in most cases? (As most
exception classes are standardized, then you have your code to catch this
info for non-standardized exceptions...)

Or missing information in cases where there was an InnerException?

The following are a couple of articles on Exception Standards:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconErrorRaisingHandlingGuidelines.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp

Hope this helps
Jay








0 Comments:

Post a Comment

 
Previous Posts
    - How to include a C# class in VB.net?
    - Datagrid events
    - Spam filter
    - Trying to use PrintForm method
    - Make a classname a variable
    - Database Selection Advice Please
    - Record Selection Formula
    - ImageCombos- how to use them?
    - Intptr indexing
    - Error converting "LenB" from vb6 to .net

Archives
    - 10/03/2004 - 10/10/2004
    - 10/10/2004 - 10/17/2004
    - 10/17/2004 - 10/24/2004
    - 10/24/2004 - 10/31/2004
    - 10/31/2004 - 11/07/2004
    - 11/21/2004 - 11/28/2004
    - 11/28/2004 - 12/05/2004
    - 12/05/2004 - 12/12/2004
    - 12/12/2004 - 12/19/2004
    - 12/19/2004 - 12/26/2004
    - 12/26/2004 - 01/02/2005
    - 01/23/2005 - 01/30/2005
    - 01/01/2006 - 01/08/2006
    - 09/24/2006 - 10/01/2006

Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland