Catching unhandled exception on separate threads

后端 未结 3 2117
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 12:34

I am using the following event to catch unhandled exceptions in the main UI thread.

Application.ThreadException 

Unfortunately, it does not

3条回答
  •  暖寄归人
    2020-11-28 13:02

    Yes, you have to manually catch exceptions on threads.

    However, this code:

    void ThreadMethod(object state)
    {
        try
        {
            ActualWorkerMethod();
        }
        catch (Exception err)
        {
            _logger.Error("Unhandled exception in thread.", err);
        }
    }
    
    void ActualWorkerMethod()
    {
        // do something clever
    }
    

    can be simplified to this using PostSharp:

    [LogExceptions]
    void ActualWorkerMethod()
    {
        // do something clever
    }
    

提交回复
热议问题