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

  • Home
  • SEARCH
  • 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 7690943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:30:42+00:00 2026-05-31T20:30:42+00:00

I have read that a using like this: using (myObject) { myObject.DoStuff(); } Can

  • 0

I have read that a using like this:

using (myObject)
{
   myObject.DoStuff();
}

Can be thought of like this:

try
{
   myObject.DoStuff();
}
finally
{
   myobject.Dispose()
}

So if myObejct.DoStuff throws ExceptionA and then myObject.Dispose() also throws an exception (ExceptionB) then ExceptionA will be lost. (See the MSDN examples here for a better description.)

Does this mean that if the code inside a using block code could throw an exception (which is most code right?) then a using statement is a bad practice?

  • 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-31T20:30:44+00:00Added an answer on May 31, 2026 at 8:30 pm

    Does this mean that if the code inside a using block code could throw an exception (which is most code right?) then a using statement is a bad practice?

    No.

    and then myObject.Dispose() also throws an exception

    This is really the crux of your question.

    This is the “bad practice” here. IDisposable.Dispose implementations should really be designed so that they don’t raise exceptions except in situations which are truly unrecoverable.

    Since IDisposable is really intended to release the resource in question, the main issue should be making sure that this implementation won’t throw under most circumstances. Having a cleanup method throw will cause a lot of grief – and is the reason why the using statement shouldn’t be used with WCF clients, etc.

    That being said, I don’t think the using statement itself is a bad practice. In fact, it tends to be a very good practice, as it avoids a very common pitfall (missing the disposable of a resource in the case of an exception).

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

Sidebar

Related Questions

Apparently I can't move files on different volumes using Directory.Move. I have read that
I have read that you can do it, but would this really improve performance
I have a query that looks like this: using (MyDC TheDC = new MyDC())
I have read that using fulltext searching is faster then using LIKE %%. I
I have read that using database keys in a URL is a bad thing
I have read that to match a word inside of a string using Regular
i have read in more then one place that using NHibernate's Identifier as Primary
Throughout a Bison grammar I am using right recursion, and I have read that
I have a config file that I read using the RawConfigParser in the standard
I have a file that I want to read in using the File::Slurp module

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.