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

  • Home
  • SEARCH
  • 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 8734783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:54:54+00:00 2026-06-13T09:54:54+00:00

With System.Threading.Tasks.Task<TResult> , I have to manage the exceptions that could be thrown. I’m

  • 0

With System.Threading.Tasks.Task<TResult>, I have to manage the exceptions that could be thrown. I’m looking for the best way to do that. So far, I’ve created a base class that manages all the uncaught exceptions inside the call of .ContinueWith(...)

I’m wondering if there’s a better way do do that. Or even if it is a good way to do that.

public class BaseClass
{
    protected void ExecuteIfTaskIsNotFaulted<T>(Task<T> e, Action action)
    {
        if (!e.IsFaulted) { action(); }
        else
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                /* I display a window explaining the error in the GUI 
                 * and I log the error.
                 */
                this.Handle.Error(e.Exception);
            }));            
        }
    }
}   

public class ChildClass : BaseClass
{
    public void DoItInAThread()
    {
        var context = TaskScheduler.FromCurrentSynchronizationContext();
        Task.Factory.StartNew<StateObject>(() => this.Action())
                    .ContinueWith(e => this.ContinuedAction(e), context);
    }

    private void ContinuedAction(Task<StateObject> e)
    {
        this.ExecuteIfTaskIsNotFaulted(e, () =>
        {
            /* The action to execute 
             * I do stuff with e.Result
             */

        });        
    }
}
  • 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-13T09:54:55+00:00Added an answer on June 13, 2026 at 9:54 am

    There are two ways you can do this, dependent on the version of the language you are using.

    C# 5.0 and above

    You can use the async and await keywords to simplify a great deal of this for you.

    async and await were introduced into the language to simplify using the Task Parallel Library, preventing you from having to use ContinueWith and allowing you to continue to program in a top-down manner.

    Because of this, you can simply use a try/catch block to catch the exception, like so:

    try
    {
        // Start the task.
        var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });
    
        // Await the task.
        await task;
    }
    catch (Exception e)
    {
        // Perform cleanup here.
    }
    

    Note that the method encapsulating the above must use have the async keyword applied so you can use await.

    C# 4.0 and below

    You can handle exceptions using the ContinueWith overload that takes a value from the TaskContinuationOptions enumeration, like so:

    // Get the task.
    var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });
    
    // For error handling.
    task.ContinueWith(t => { /* error handling */ }, context,
        TaskContinuationOptions.OnlyOnFaulted);
    

    The OnlyOnFaulted member of the TaskContinuationOptions enumeration indicates that the continuation should only be executed if the antecedent task threw an exception.

    Of course, you can have more than one call to ContinueWith off the same antecedent, handling the non-exceptional case:

    // Get the task.
    var task = new Task<StateObject>(() => { /* action */ });
    
    // For error handling.
    task.ContinueWith(t => { /* error handling */ }, context, 
        TaskContinuationOptions.OnlyOnFaulted);
    
    // If it succeeded.
    task.ContinueWith(t => { /* on success */ }, context,
        TaskContinuationOptions.OnlyOnRanToCompletion);
    
    // Run task.
    task.Start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a collection of System.Threading.Tasks.Task : HashSet<Task> myTasks = new HashSet<Task>();
In my WPF Window_Loaded event handler I have something like this: System.Threading.Tasks.Task.Factory.StartNew(() => {
Is there a way to cancel an Async System.Threading.Tasks.Task? i.e. Task.Factory.FromAsync( client.BeginCallWebService, client.EndCallWebService, param1,
As some may have seen in .NET 4.0, they've added a new namespace System.Threading.Tasks
I've read about advantages of Tasks Difference between Task (System.Threading.Task) and Thread Also msdn
I am using a System.Threading.ThreadPool to manage a queue of jobs from a service.
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
I have an issue with System.Threading.Timer. I am scheduling some actions using a time
I'm developing an app that starts a System.Threading.Timer which does some fairly rapid reading/writing
Ok, playing around with the .Net 4.0 Parellel Extensions in System.Threading.Tasks. I'm finding what

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.