Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:21:28+00:00 2026-06-12T00:21:28+00:00

I noticed that exceptions thrown from a thread other than my main application thread

  • 0

I noticed that exceptions thrown from a thread other than my main application thread are not caught by the DispatcherUnhandledException event handler. So I have to manually throw them like this:

Task.Factory.StartNew(() =>
{
    throw new Exception("oops! something went wrong...");

}).ContinueWith((task) =>
{
    if (task.IsFaulted)
    {
        App.Current.Dispatcher.Invoke(new Action(() =>
        {
            throw task.Exception.InnerExceptions.First();
        }));
    }
});

However, I don’t want to add the above ContinueWith method to every single Task I create. I would rather have some way for this to be handled automatically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:21:30+00:00Added an answer on June 12, 2026 at 12:21 am

    The following class solves this problem:

    /// <summary>
    /// Extends the System.Threading.Tasks.Task by automatically throwing the first exception to the main application thread.
    /// </summary>
    public class TaskEx
    {
        public Task Task { get; private set; }
    
        private TaskEx(Action action)
        {
            Task = Task.Factory.StartNew(action).ContinueWith((task) =>
            {
                ThrowTaskException(task);
            });
        }
    
        public static TaskEx StartNew(Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException();
            }
    
            return new TaskEx(action);
        }
    
        public TaskEx ContinueWith(Action<Task> continuationAction)
        {
            if (continuationAction == null)
            {
                throw new ArgumentNullException();
            }
    
            Task = Task.ContinueWith(continuationAction).ContinueWith((task) =>
            {
                ThrowTaskException(task);
            });
    
            return this;
        }
    
        private void ThrowTaskException(Task task)
        {
            if (task.IsFaulted)
            {
                App.Current.Dispatcher.Invoke(new Action(() =>
                {
                    throw task.Exception.InnerExceptions.First();
                }));
            }
        }
    }
    

    Now I can simply use the following code (exactly the same as the Task class):

    TaskEx.StartNew(() =>
    {
        // do something that may cause an exception
    }).ContinueWith((task) =>
    {
        // then do something else that may cause an exception
    }).ContinueWith((task) =>
    {
        // then do yet something else that may cause an exception
    });
    

    Unlike the Task class, however, any exception thrown from one of these threads will be AUTOMATICALLY caught by my DispatcherUnhandledException event handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that IE 9 does not support application cache. How can I use
Possible Duplicate: Why are Exceptions not Checked in .NET? Coming from Eclipse/Java, I noticed
I noticed that the first creation of an image from an uri takes pretty
I noticed that I can't combine --traditional options with the other one letter other
I noticed that median of boxplot (constructed with restricted ylim-parameter) may differ from the
I have an Unhandled Exception handler that does something like this: AddHandler Application.ThreadException, AddressOf
So I was writing some code and I noticed that apart from syntactical, type,
While playing with exception handling in Java I noticed that no exception is thrown
I have noticed that when I call the /install flag on NServiceBus.host.exe an event
I am working in FireFox and I notice that a exception is thrown in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.