How to catch Exception that throw by timer

B

bob

I have a console application and a timer inside.

The timer will throw an Exception while event Elapsed.
but i cannot catch this Excepion,
How can I catch the Exception?
 
M

Marc Gravell

Well, most commonly it would be the *handler* that it throwing the
exception; so can you not just add a try/catch to the method you are
using to handle the event? Or change the event-subscription to attach
a new method that just does a try/catch around the old handler? i.e.
change
t.Elapsed += OriginalHandler;
to
t.Elapsed += NewHandler;
with
[static?] void NewHandler(object sender, ElapsedEventArgs e)
{
try
{
OriginalHandler(sender, e);
}
catch (Exception ex)
{
// log etc
}
}

Marc
 

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