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 7691015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:31:43+00:00 2026-05-31T20:31:43+00:00

http://msdn.microsoft.com/en-us/library/dd997415.aspx Per the article referenced above I am trying to handle exceptions in a

  • 0

http://msdn.microsoft.com/en-us/library/dd997415.aspx

Per the article referenced above I am trying to handle exceptions in a continuatin task. The example I am citing in the above article is this:

var task1 = Task.Factory.StartNew(() =>
{
    throw new MyCustomException("Task1 faulted.");
})
.ContinueWith((t) =>
    {
        Console.WriteLine("I have observed a {0}",
            t.Exception.InnerException.GetType().Name);
    },
    TaskContinuationOptions.OnlyOnFaulted);

My code is:

Task<string> task = Task<string>.Factory.StartNew(() => process.StartTask(this));
task.ContinueWith(CloseDialog, TaskContinuationOptions.OnlyOnFaulted);

In StartTask, I throw an error just like the example. My expectation is that CloseDialog will execute and I can examine task.Exception within that method as shown by the example. However when I throw the exception I the code simply stops with an unhandled exception. Should I be using a try/catch block? If so, where?
By the way, I want my continuation task (CloseDialog) to ALWAYS run. I’m just using .OnlyOnFaulted because that is what is shown in the example.

  • 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-05-31T20:31:44+00:00Added an answer on May 31, 2026 at 8:31 pm

    A continuation can find out if an exception was thrown by the antecedent Task by the antecedent task’s exception property. The following prints the results of a NullReferenceException to the console

    Task task1 = Task.Factory.StartNew (() => { throw null; });
    Task task2 = task1.ContinueWith (ant => Console.Write(ant.Exception());
    

    If task1 throws an exception and this exception is not captured/queried by the continuation it is considered unhandled and the application dies. With continuations it is enough to establish the result of the task via the Status keyword

    asyncTask.ContinueWith(task =>
    {
        // Check task status.
        switch (task.Status)
        {
            // Handle any exceptions to prevent UnobservedTaskException.             
            case TaskStatus.RanToCompletion:
                if (asyncTask.Result)
                {
                    // Do stuff...
                }
                break;
            case TaskStatus.Faulted:
                if (task.Exception != null)
                    mainForm.progressRightLabelText = task.Exception.InnerException.Message;
                else
                    mainForm.progressRightLabelText = "Operation failed!";
            default:
                break;
        }
    }
    

    If you don’t use continuations you either have to wait on the task in a try/catch block or query a task’s Result in a try/catch block

    int x = 0;
    Task<int> task = Task.Factory.StartNew (() => 7 / x);
    try
    {
        task.Wait();
        // OR.
        int result = task.Result;
    }
    catch (AggregateException aggEx)
    {
        Console.WriteLine(aggEx.InnerException.Message);
    }
    

    Hope this help even if it is a bit late and you know all there is by now! :]

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

Sidebar

Related Questions

http://msdn.microsoft.com/en-us/library/hh680905(v=pandp.50).aspx Transient Fault Handling gives the ability to handle connection errors etc. SaveChangesWithRetries() says
This msdn article -- http://msdn.microsoft.com/en-us/library/bb219746(VS.85).aspx#Disabling_Accessibility_Shortcut_Keys -- provides information for C++ programmers on how to
According to http://msdn.microsoft.com/en-us/library/ms687032%28v=vs.85%29.aspx , WaitForSingleObject() has undefined behavior if a handle gets closed while
As per http://msdn.microsoft.com/en-us/library/h21twfw7(v=VS.100).aspx (for Visual Studio 2010) strstreambuf is deprecated. I was of the
This article here http://msdn.microsoft.com/en-us/library/bb188204%28v=sql.90%29.aspx?ppud=4 describes how to log SQL requests sent via ODBC. Does
I was following this article: http://msdn.microsoft.com/en-us/library/aa902637%28v=sql.80%29.aspx and my query for distinct count looks like
http://msdn.microsoft.com/en-us/library/ms181765.aspx I see the sql below from above link: SELECT BusinessEntityID, SalariedFlag FROM HumanResources.Employee
http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx If you create a custom algorithm that determines whether two floating-point numbers can
http://msdn.microsoft.com/en-us/library/ms189797.aspx In this link they are committing a transaction within catch clause IF (XACT_STATE())
According to MSDN ( http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autosize.aspx ), there's a note about Label 's AutoSize property:

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.