try
{
    string s = null;
    s.PadLeft(10);
}
catch (Exception ex)
{
    // send exception to UI Thread so it can be handled by our global exception
    // handler
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Send,
        new Action<Exception>(e => { throw ex; }), ex);
}

如您所见,'throw ex' 将截断堆栈跟踪,我想使用 throw 而不是 throw ex 但我得到:



如何使用 lambda 表达式抛出异常而不截断堆栈跟踪?

最佳答案

为什么不创建一个新的 Exception 并将旧的异常作为 InnerException 呢?

e => throw new WhateverException("your message", ex);

这保留了原始堆栈跟踪。

关于c# - WPF 调度程序线程 - 使用 lambda 表达式并抛出将异常调度到 UI 线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8338305/

10-13 09:26