How to catch Exception that throw by timer

  • Thread starter Thread starter bob
  • Start date Start date
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?
 
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
 
Back
Top