Get derived class from base class (find actual exception from Exception)?

Z

Zytan

I deal with a bunch of exception handlers by calling a function that
writes the info to a log file. Of course, this takes in Exception, so
I can pass it any exception class. But, it'd be great if somehow I
could state what the type of the exception it was. I do know that I
cannot really make use of it, since this function would have to know
about ALL possible derived classes, which it cannot. And I do know
that Exception.Message exists to print out. But, I was just thinking
if there's anything else I could do, in such a generic function, to
help the reader of the log file understand what happened.

Any thoughts are welcome.

Zytan
 
N

Nicholas Paldino [.NET/C# MVP]

Zytan,

Perhaps you can use reflection to print out the name of the type of the
exception, as well as the names of the members, and the values of the public
properties exposed by the type. This way, it will have more information
than just knowing that an exception occured.

Also, you are keeping a log. Is there a reason you are coding this
yourself instead of not using one of the existing, free logging components
out there? Log4net is a popular one, and there is a Logging Application
Blog in the Enterprise Library offered from Microsoft.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Zytan said:
I deal with a bunch of exception handlers by calling a function that
writes the info to a log file. Of course, this takes in Exception, so
I can pass it any exception class. But, it'd be great if somehow I
could state what the type of the exception it was.

I suspect you're after the GetType method:

void Log(Exception e)
{
log.Write ("Exception was of type {0}", e.GetType());
}
 
Z

Zytan

I suspect you're after the GetType method:
void Log(Exception e)
{
log.Write ("Exception was of type {0}", e.GetType());
}

YES! Thanks, Jon! I could actually make it deal with specific
exceptions that I expect differently, now.

Zytan
 
Z

Zytan

Perhaps you can use reflection to print out the name of the type of the
exception, as well as the names of the members, and the values of the public
properties exposed by the type. This way, it will have more information
than just knowing that an exception occured.

Yes, if I coul print out all information the exception has, no matter
what type of derived exception it is, this is exactly what I want.
The more info, the better. This will be used primarily for 1 reason:
When the program crashes due to an unhandled exception, I hope to
catch this (there's another thread here I started about that), and
report the information, so it can be solved later.
Also, you are keeping a log. Is there a reason you are coding this
yourself instead of not using one of the existing, free logging components
out there? Log4net is a popular one, and there is a Logging Application
Blog in the Enterprise Library offered from Microsoft.

Several reasons: 1. because I started it myself before I knew about
this, 2. mine was going to be simple, 3. I'm too stubborn to change
now, 4. I enjoy learning how to do this myself by coding rather than
reading other people's code (after all, there's only so much you can
learn from wisemen, the rest comes from experience). 5. My logger is
99% complete, and does just what I want.

Zytan
 

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