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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:33:33+00:00 2026-06-06T16:33:33+00:00

The using construct looks incredibly handy for situations that require both beginning and separated

  • 0

The “using” construct looks incredibly handy for situations that require both beginning and separated end parts.

Quick example to illustrate:

using (new Tag("body")) {
    Trace.WriteLine("hello!");
}
// ...
class Tag : IDisposable {
    String name;
    public Tag(String name) {
        this.name = name;
        Trace.WriteLine("<" + this.name + ">");
        Trace.Indent();
    }
    public void Dispose() {
        Trace.Unindent();
        Trace.WriteLine("</" + this.name + ">")
    }
}

The beginning part is defined as the constructor, the end part is the Dispose method.

However despite of being attractive this construct has a serious caveat that comes from the fact that the Dispose method is called from within a finally block. So there are 2 problems:

  1. You should avoid throwing exceptions from the finally block because they will override the original exception that was supposed to be caught.

  2. There is no way of knowing inside of the Dispose method if an exception was thrown earlier between “beginning” and “end” and thus there is no way of handling the “end” part accordingly.

These 2 things make using of this construct impractical which is a very sad fact.
Now, my questions are:

  1. Is my understanding of the problems right? Is this how “using” actually works?

  2. If so, is there any way to overcome these problems and make practical use of the “using” construct other than what it was originally designed for (releasing resources and cleaning up)

  3. In case there is no practical way for “using” to be used this way. What are the alternative approaches (to enforce the context over some code with the beginning and end parts)?

  • 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-06-06T16:33:35+00:00Added an answer on June 6, 2026 at 4:33 pm

    The intent of the using statement and of the IDisposable interface is for the user to dispose of unmanaged resources. These resources are usually expensive and precious, so they must be disposed of no matter what (that’s why it’s on the finally). Code in finally blocks can’t even be aborted, and it can hang a whole app domain shutdown.

    Now, it’s very tempting to abuse using for the purposes you’re describing, and I’ve done that in the past. Most of the time there’s no danger there. But if an unexpected exception happens, the whole state of the processing is compromised, you wouldn’t necessarily want to run the end operation; so in general, don’t do this.

    An alternative is to use a lambda, something like this:

    public interface IScopable { 
      void EndScope();
    }
    
    public class Tag : IScopable {
      private string name;
      public Tag(string name) {
        this.name = name;
        Trace.WriteLine("<" + this.name + ">");
        Trace.Indent();
      }
      public void EndScope() {
        Trace.Unindent();
        Trace.WriteLine("</" + this.name + ">");
      }
    }
    
    public static class Scoping {
      public static void Scope<T>(this T scopable, Action<T> action) 
        where T : IScopable {
        action(scopable);
        scopable.EndScope();
      }
    }
    

    Use it like this:

    new Tag("body").Scope(_ => 
      Trace.WriteLine("hello!")
    );
    

    You can also create other implementations that run certain actions based on whether exceptions were raised of not.

    In Nemerle, the language can be extended with new syntax to support this.

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

Sidebar

Related Questions

I'm using a java based uploading construct http://www.javaatwork.com/java-upload-applet/details.html that I tried running over night.
I'm using Report Builder 2.0 to construct a report that contains datetime data stored
I'm trying to construct a JDOQL query (using datanucleus) that will search for matches
I am using the basic login manager that is in the example bugs application
I am using the ObservableSortedDictionary from Dr. WPF. The constructor looks like this: public
How to construct a GQL query using server side admin datastore viewer, a one
I am using a java PreparedStatment object to construct a series of batched INSERT
How can I construct an object using a query if I also need to
I am using pymongo and trying to find the best way to construct classes.
I am using the Zend framework and what I need is to construct a

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.