Exception Handling Question: Event Logging

E

ETL

Hi,
I think this question has an easy answer but I can't seem to find
anything online that lists best practices for my problem.

I have a C# app that uses an exception handling tree similar to the
following.

Try
{
blah blah blah....
}
catch(UnauthorizedAccessException noAuth){}
catch(IndexOutOfRangeException){}
catch(Exception Ex){}
finally{}

I want to log an event on my server if any of the following exceptions
are thrown. My server logging methog looks like this:

LoggingManager.LogServerEvent(Exception.Message.ToString());

I can add this method to each catch clause and it alogs the event fine.
I'd like to optimize my code so that this logging method is only in one
place. It seems silly to put it in all the catch clauses. Basically, if
ANY exceptions was raised, log it.

Any suggestions?

Thanks

Eric
 
J

jeremiah johnson

I think you'll have to catch it in each catch block. unless there's a
way to trigger an event on any exception, then use that event to log the
exception, you'll have to put it in each place.

that's my admittedly limited experience, anyway.
 
B

bart_deboeck

Hi Eric,

You did not give all the details of the blah blah part of your code,
now based on the limited information :
- why do you have to catch three types of exceptions ? can you
refactor your blah blah part of the code ? it seems to me that this
method has different roles, granting access, getting elements from an
array, etc
- it is sufficient to catch Exception and log the Message, you will
have the same functionality as you have know (Exception is the base of
the other exceptions)
- check the exception handling on
http://www.microsoft.com/downloads/...09-660e-444e-945a-6b32af1581b3&displaylang=en

success,
Bart

http://www.xenopz.com
 

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