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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:33:20+00:00 2026-05-28T04:33:20+00:00

I need to translate an exception emanating from a Task<T> in the same manner

  • 0

I need to translate an exception emanating from a Task<T> in the same manner that doing the following would for normal synchronous code:

try {
  client.Call();
} catch(FaultException ex) {
    if (ex.<Some check>)
        throw new Exception("translated");
}

However, I want to do this asynchronously, i.e., Call above is actually Task CallAsync().

So in C# 5 my method would look like this:

async Task CallAndTranslate()
{
    try{
        await client.CallAsync();
    } catch(FaultException ex) {
        if (ex.FaultCode ...)
            throw new Exception("translated");
    }
}

But I’m using C# 4 for now.

So what can I do given that I want to trigger a Task but have the (TPL) Fault be translated and then expose the whole thing once again as a Task<T>?

  • originally emanating from a WCF webservice but that’s not important here

EDIT: A slightly more concrete way of saying it:

public class TranslatingExceptions
{
    public Task ApiAsync() // The inner layer exposes it exactly this way
    {
        return Task.Factory.StartNew(()=>{ throw new Exception( "Argument Null" );});
    }

    public Task WrapsApiAsync() // this layer needs to expose it exactly this way
    {
        // async preview pseudocode for what I need to do                            
        try {
            await ApiAsync(  );
        } catch (Exception exception){
            if( exception.Message == "Argument Null"  )
                throw new ArgumentNullException();
        }
    }

    [Fact]
    public void Works()
    {
        var exception = Record.Exception( () => 
            WrapsApiAsync().Wait());
        Assert.IsType<ArgumentNullException>( exception.InnerException);
    }
}

How would you implement WrapsApiAsync() without needing C# 5?

  • 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-28T04:33:21+00:00Added an answer on May 28, 2026 at 4:33 am

    Ok, so now that I’m entirely clear on what you’re looking for, here’s all you would need to do to build the equivalent in 4.0:

    public class TranslatingExceptions
    {
        public Task ApiAsync() // The inner layer exposes it exactly this way
        {
            return Task.Factory.StartNew(()=>{ throw new Exception( "Argument Null" );});
        }
    
        public Task WrapsApiAsync() // this layer needs to expose it exactly this way
        {
            // Grab the task that performs the "original" work
            Task apiAsyncTask = ApiAsync();
    
            // Hook a continuation to that task that will do the exception "translation"
            Task result = aspiAsync.ContinueWith(antecedent =>
            {
                // Check if the antecedent faulted, if so check what the exception's message was
                if ( antecedent.IsFaulted 
                  && antecedent.Exception.InnerException.Message == "Argument Null" )
                {
                    throw new ArgumentNullException();
                }
            },
            TaskContinuationOptions.ExecuteSynchronously);
    
            // Now we return the continuation Task from the wrapper method so that the caller of the wrapper method waits on that
            return result;
        }
    
        [Fact]
        public void Works()
        {
            var exception = Record.Exception(() => 
                                             WrapsApiAsync().Wait());
    
            Assert.IsType<ArgumentNullException>(exception.InnerException);
        }
    }
    

    This should accomplish what you’re looking for. One thing to note is that I use TaskContinuationOptions.ExecuteSynchronously when creating the continuation. This is because this work is small and tight and you don’t want to incur the over head of waiting for a whole other thread having to be picked up from the thread pool by the scheduler just to do this check.

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

Sidebar

Related Questions

I need to translate the following code to C# from php. What libraries /
I have a code that need to be translated from C to Cpp, and
i need to translate following C# method to the same IronPhyton method private void
I need to translate some code from Scheme to Common Lisp. Now, I have
I need to translate this piece of code from Perl to Lua open(FILE, '/proc/meminfo');
I have a case where I need to translate (lookup) several values from the
I have a task where I need to translate a DataTable to a two-dimensional
Im a coding a library including textual feedback that I need to translate. I
I need to translate colors in bitmap loaded to BufferedImage from RGB to YCbCr
I have a C# web method that I need to translate to VB.NET. The

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.