Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Dispatcher Thread- Using lambda expression and throw to dispatch exception to UI thread

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);
}

As you can see, 'throw ex' will truncate the stacktrace, I would like to use throw instead of throw ex but I get:

A throw statement with no arguments is not allowed outside of a catch clause.

How can I use lambda expression to throw the exception without truncating stacktrace?

like image 918
Syler Avatar asked Jun 05 '26 14:06

Syler


1 Answers

Why don't you just create a new Exception with the old exception as InnerException?

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

That preserves the original stacktrace.

like image 156
Botz3000 Avatar answered Jun 07 '26 02:06

Botz3000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!