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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:54:39+00:00 2026-06-08T14:54:39+00:00

somehow my exceptions seem to being caught by method they are executing in. Here

  • 0

somehow my exceptions seem to being caught by method they are executing in. Here is the code to call the method. As you can see I create a cancellation token with a time out. I register a method to call when the cancellation token fires and then I start a new task. The cancellation token appears to be working OK. As does the registered method.

var cancellationToken = new CancellationTokenSource(subscriber.TimeToExpire).Token;
cancellationToken.Register(() =>
{
    subscriber.Abort();
});
var task = Task<bool>.Factory.StartNew(() =>
{                  
 subscriber.RunAsync((T)messagePacket.Body, cancellationToken);
 return true;
})
.ContinueWith(anticedant => 
{
    if (anticedant.IsCanceled)
    {
        Counter.Increment(12);
        Trace.WriteLine("Request was canceled");
    }

    if (anticedant.IsFaulted)
    {
        Counter.Increment(13);
        Trace.WriteLine("Request was canceled");
    }

    if (anticedant.IsCompleted)
    {
        Counter.Increment(14);
    }

The next piece of code is the method that seems to be not only throwing the excetion (expected behavior. but also catching the exception.

public async override Task<bool> ProcessAsync(Message input, CancellationToken cancellationToken)
{
    Random r = new Random();
    Thread.Sleep(r.Next(90, 110));
    cancellationToken.ThrowIfCancellationRequested();
    return await DoSomethingAsync(input);
}

The exception is being thrown by the cancellation token but according to intellitrace it is being caught at the end of the method. I have tried a number of different options including throwing my own exception, but no matter what the continuewith function always executes the IsComleted or ran to completion code.
Any ideas on what I am doing wrong?
Thanks

  • 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-08T14:54:41+00:00Added an answer on June 8, 2026 at 2:54 pm

    I assume that RunAsync is the same as ProcessAsync.

    The exception is being thrown by the cancellation token but according to intellitrace it is being caught at the end of the method.

    Yup. Any async method will catch its own exceptions and place them on its returned Task. This is by design.

    no matter what the continuewith function always executes the IsComleted or ran to completion code.

    Well, let’s take another look at the code:

    var task = Task<bool>.Factory.StartNew(() =>
    {                  
      subscriber.RunAsync((T)messagePacket.Body, cancellationToken);
      return true;
    })
    .ContinueWith(
    

    Consider the lambda passed to StartNew: it calls RunAsync, it ignores the Task that it returns, and then it returns true (successfully). So the Task returned by StartNew will always return successfully. This is why the ContinueWith always executes for a successfully-completed task.

    What you really want is to await the Task returned by RunAsync. So, something like this:

    var task = Task.Run(async () =>
    {                  
      await subscriber.RunAsync((T)messagePacket.Body, cancellationToken);
      return true;
    })
    .ContinueWith(
    

    You’re still ignoring the return value of RunAsync (the bool it returns is ignored), but you’re not ignoring the Task itself (including cancellation/exception information).

    P.S. I’m assuming there’s a lot of code you’re not showing us; using StartNew/Run to just kick off some async work and return a value is very expensive.

    P.P.S. Use await Task.Delay instead of Thread.Sleep.

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

Sidebar

Related Questions

Suppose I want to wrap code that can throw exceptions with a try-catch block
Somehow my code doesn't work any more (it did work before with the exact
somehow I get never any results when I call: select * from table_1 t1
I have error handling code that is designed to catch unnhandled exceptions, and then
I knew my code was dying somehow because of the other symptoms, but even
I understand that Exceptions are for exceptional cases [a], but besides just being repeated
Here's some code that catches an exception thrown on the Event Dispatch Thread: package
I have some code that needs to rescue multiple types of exceptions in ruby:
I have client-server (Silverlight) app. Some server code throws exceptions that I handle on
I've run into a couple different exceptions with this block of code in one

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.