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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:01:57+00:00 2026-05-11T12:01:57+00:00

I am looking for advice as to how to handle any exceptions thrown in

  • 0

I am looking for advice as to how to handle any exceptions thrown in the following code example:

private string SendRequest() {         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myURL);         // Code initialising HttpWebRequest         HttpWebResponse response = (HttpWebResponse)request.GetResponse();         Stream rcvdStream = response.GetResponseStream();         StreamReader readStream = new StreamReader(rcvdStream, Encoding.UTF8);         string responseString = readStream.ReadToEnd();         response.Close();         readStream.Close();         return responseString; } 

My main concern is to ensure the StreamReader and HttpRequest object are closed whent the method ends. Should I:

  1. Wrap the lot up in a try/catch/finally logging any exceptions in the catch block and closing the stream in the finally block?
  2. Use a using statement on the HttpWebRequest object instantiation and a nested using statement when creating the StreamReader?
  3. Not worry about it and assume GC will clear everything up when the objects go out of scope as the method is exited?

EDIT: Further investigation has revealed that option 2 can be done without nesting the using statements:

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())     using (Stream rcvdStream = response.GetResponseStream())     {         StreamReader readStream = new StreamReader(rcvdStream, Encoding.UTF8);         payResponse = readStream.ReadToEnd();     } 

This produces the following IL code which demonstrates that it is effectively creating a nested try/finally block:

  IL_00b0:  callvirt   instance class [System]System.Net.WebResponse [System]System.Net.WebRequest::GetResponse()   IL_00b5:  castclass  [System]System.Net.HttpWebResponse   IL_00ba:  stloc.s    response   .try   {     IL_00bc:  ldloc.s    response     IL_00be:  callvirt   instance class [mscorlib]System.IO.Stream [System]System.Net.WebResponse::GetResponseStream()     IL_00c3:  stloc.s    rcvdStream     .try     {       IL_00c5:  nop       IL_00c6:  ldloc.s    rcvdStream       IL_00c8:  call       class [mscorlib]System.Text.Encoding [mscorlib]System.Text.Encoding::get_UTF8()       IL_00cd:  newobj     instance void [mscorlib]System.IO.StreamReader::.ctor(class [mscorlib]System.IO.Stream,                                                                                  class [mscorlib]System.Text.Encoding)       IL_00d2:  stloc.s    readStream       IL_00d4:  ldloc.s    readStream       IL_00d6:  callvirt   instance string [mscorlib]System.IO.TextReader::ReadToEnd()       IL_00db:  stloc.3       IL_00dc:  nop       IL_00dd:  leave.s    IL_00f3     }  // end .try     finally     {       IL_00df:  ldloc.s    rcvdStream       IL_00e1:  ldnull       IL_00e2:  ceq       IL_00e4:  stloc.s    CS$4$0001       IL_00e6:  ldloc.s    CS$4$0001       IL_00e8:  brtrue.s   IL_00f2       IL_00ea:  ldloc.s    rcvdStream       IL_00ec:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()       IL_00f1:  nop       IL_00f2:  endfinally     }  // end handler     IL_00f3:  nop     IL_00f4:  leave.s    IL_010a   }  // end .try   finally   {     IL_00f6:  ldloc.s    response     IL_00f8:  ldnull     IL_00f9:  ceq     IL_00fb:  stloc.s    CS$4$0001     IL_00fd:  ldloc.s    CS$4$0001     IL_00ff:  brtrue.s   IL_0109     IL_0101:  ldloc.s    response     IL_0103:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()     IL_0108:  nop     IL_0109:  endfinally   }  // end handler 
  • 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. 2026-05-11T12:01:58+00:00Added an answer on May 11, 2026 at 12:01 pm

    Options one and two are your best bets. Option 3 IS NOT a good idea.

    I am personally quite fond of the Using statement route. However, the try/catch/finally route is essentially the same, and provides you your needed logging mechanism.

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

Sidebar

Related Questions

I'm looking for advice on how best to handle Signalr hub connections in a
I was looking for any suggestion or a good approach to handle messages between
I'm looking for some advice on how to optimise the following process: App reads
Is there any way to automatically handle cookies in .NET with the HttpWebRequest/HttpWebResponse objects?
Looking for advice on framework and approach for building a modular web application. Primary
I am looking for advice about how to parse a single list, using two
I'm looking for advice, tutorials and links at how to set up a mid-sized
Overview I'm looking for advice / solutions on standardizing color profiles on comps so
Looking for best advice on how to do this: I have an insert like
Looking for some advice on the best way to implement localization along with client

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.