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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:22:32+00:00 2026-05-15T22:22:32+00:00

When using asynchronous code to read from streams etc using the BeginXXX / EndXXX

  • 0

When using asynchronous code to read from streams etc using the BeginXXX / EndXXX pattern, I believe that any exceptions that occur during the process will be thrown when the call to EndXXX is made.

Does this mean that the initial call to BeginXXX will never throw an exception, it will always be thrown by EndXXX?

Or to put it another way, should I enclose BeginRead with try{}catch{} as well?

public StartReading()
{
        // Should this be enclosed with try{}catch{} ?
        stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(readCallback), stream);
}

private void readCallback(IAsyncResult result)
{
    Stream stream = (Stream)result.AsyncState;

    try
    {
        int len = stream.EndRead(result);

        // Do work...

    }
    catch(Exception ex)
    {
        // Error handling stuff.
    }
}
  • 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-15T22:22:33+00:00Added an answer on May 15, 2026 at 10:22 pm

    Well, any code can throw an exception, so “never” is strong… for example, OutOfMemoryException, ThreadAbortException, or some other exception that indicates resource saturation (for example, it somehow can’t start the async operation).

    It might (although I haven’t tested) also throw immediately if that is a write-only stream. And it will certainly throw immediately if stream turns out to be null.

    However! In all the cases I’ve mentioned, the correct behaviour is probably to let it bubble up; they all indicate pretty fundamental problems unrelated to the current logic. So no: I wouldn’t try/catch here unless there was something specific I expected and wanted to handle somehow.

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

Sidebar

Related Questions

I have seen (and read) about using Dispatcher.BeginInvoke to ensure that UI updates occur
I tried to make an asynchronous UDP client using boost::asio I get some code
I am running some import code asynchronously from a simple WinForms app using a
I know that it's possible to implicitly provide asynchronous interaction using: Asynchronous Delegates Asynchronous
I'm writing some code with boost::asio , using asynchronous TCP connections. I've to admit
What is the pattern and/or how would you change the following code so that
I am writing some code that downloads a file from a network resource to
I'm writing an asynchronous server and client with C#. I took sample code from
I want to read from a socket in an asynchronous way. If I used
I am using this code to write asynchronously to a file public static void

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.