在一个应用程序中,我注册了EventLog.EntryWritten事件,并使用了一个.NET线程池线程来等待更改事件发生。我认为在这个.NET线程池线程中发生了一个异常,所以我想使用try/catch来确保,如下所示:
try {
eventLog.EntryWritten += new EntryWrittenEventHandler(EventLogMonitor);
}
catch (System.Componentmodel.Win32exception e)
{
// log error
}我只是想知道我是否将try/ catch放在了正确的位置来监控这个线程的异常?
发布于 2011-12-15 00:05:02
您编写的代码将在注册事件时捕获异常,而不是事件本身。
您应该将try..catch块放在EventLogMonitor方法中。
private void EventLogMonitor (Object sender,EntryWrittenEventArgs e)
{
try
{
// your logic goes here
}
catch (Exception ex)
{
//do something with the excpetion
}
}https://stackoverflow.com/questions/8507581
复制相似问题