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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:19:25+00:00 2026-06-09T18:19:25+00:00

Hi I have the following code: try { var t1 = Task.Factory.StartNew(() => {

  • 0

Hi I have the following code:

    try
    {
        var t1 = Task.Factory.StartNew(() =>
        {
            Count(5, 10);
        });
        //t1.Wait(); //This line if uncommented causes the exception to be handled below....
        Console.WriteLine("done");
    }
    catch (AggregateException ex)
    {
        Console.WriteLine(ex);
    }

    private void Count(int start, int end)
    {
        for (var i = start; i <= end; i++)
        {
            Console.WriteLine(i);
            if (i == 7) throw new InvalidOperationException("Something bad happened");
            Thread.Sleep(1000);
        }
    }

If I dont want to wait on my task, but still want to handle exceptions, how can I achieve this?

  • 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-09T18:19:27+00:00Added an answer on June 9, 2026 at 6:19 pm

    Create a continuation that handles errors when the task fails:

    var t1 = Task.Factory.StartNew(() =>
        {
            Count(5, 10);
        });
    t1.ContinueWith(
        (task) =>
        {
            var error = task.Exception;
            // handle error
        }, TaskContinuationOptions.OnlyOnFaulted);
    

    OnlyOnFaulted specifies that the continuation will run on a threadpool thread if the task fails. If the task completes successfully, then it won’t execute.

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

Sidebar

Related Questions

I have the following code: try { using (var myHttpWebResponse = (HttpWebResponse) httPrequestCreated.GetResponse()) {
I have the following code that runs without throwing an exception: var t =
I have the following code: var wqry:TAdoQuery; ... FillChar(wSpaces,cSpacesAfter,' '); try wqry := TADOQuery.Create(nil);//here
I have the following code var aVar = sample text; alert(aVar); $(this).replaceWith( <input type='text'
I have the following code try { using (var connection = new SqlConnection(Utils.ConnectionString)) {
I have the following code: private function uploadFile(file:File, packageId:String):void { try { var fullpath:String
We have following code: try { // some code throwing MyException } catch (MyException
How I can start reading from a specific byte? I have following code: try
I have the following code: try: redirect_url = self.auth.get_authorization_url() webbrowser.open_new(redirect_url) except tweepy.TweepError: print 'Error!
Hy ... I have the following code : try { Nrtrde record = new

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.