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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:04:05+00:00 2026-05-22T17:04:05+00:00

I am working on some code to use HttpWebRequest asynchronously. If any of you

  • 0

I am working on some code to use HttpWebRequest asynchronously. If any of you have ever done this before, then you know that error handling can be a bit of a pain because if an exception is thrown in one of the callback methods, it can’t be passed back to the calling code via a try/catch block.

What I want to do is handle errors by saving exceptions in my state object that gets passed to each callback method. If an exception is caught, the state object will be updated and then the http call will be aborted. The problem I have is that in my state object, I have to use an Exception property so that any type of exception can be stored. When the calling code checks the state object and “sees” an Exception, it doesn’t know what type of exception it is.

Is there a way to allow my state object to hold any type of exception, but still keep the exception strongly-typed?

State Object

public class HttpPostClientAsyncModel
    {
        public HttpResponseSnapshot Response { get; set; }
        public HttpPostClientAsyncStatus Status { get; set; }
        public Exception Exception { get; set; }
        public WebRequest Request { get; set; }
    }
  • 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-22T17:04:05+00:00Added an answer on May 22, 2026 at 5:04 pm

    The exception object is still strongly typed and retains its original field values. All you need is to check it like this:

    if (asyncModel.Exception is ArgumentException)
    {
      // Handle argument exception here
      string invalidParameter = (asyncModel.Exception as ArgumentException).ParamName;
    }
    else if (...)
    {
    }
    

    You would normally do a very similar check with try/catch block anyway so this shouldn’t be inconvenient. If you’re really worried about this, just create a new thread with the sync methods and handle the exception with continuation options:

    Task.Factory.StartNew(() => { DoWork(); })
    .ContinueWith(t => Logger.Error("An exception occurred while processing. Check the inner exception for details", t.Exception),
    TaskContinuationOptions.OnlyOnFaulted);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with some legacy code that makes extensive use of this kind of
I am working on some code coverage for my applications. Now, I know that
I'm working on some code that calls a service. This service call could fail
I have been working on some legacy C++ code that uses variable length structures
I have written some code that makes use of an open source library to
I'm working on some code that generates an Excel spreadsheet server-side and then downloads
I have some working code that does amplitude modulation and plots it. However I'm
I have some code that I use to fill a view with a linear
I have been working on some code, which I use git to manage. Earlier,
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how

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.