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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:09:54+00:00 2026-05-16T00:09:54+00:00

using(SomeClass x = new SomeClass(c:/temp/test.txt)) { … } Inside the using block, all is

  • 0
using(SomeClass x = new SomeClass("c:/temp/test.txt"))
{
...
}

Inside the using block, all is fine with treating exceptions as normal. But what if the constructor of SomeClass can throw an exception?

  • 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-16T00:09:54+00:00Added an answer on May 16, 2026 at 12:09 am

    Yes, this will be a problem when the constructor throws an exception. All you can do is wrap the using block within a try/catch block. Here’s why you must do it that way.

    using blocks are just syntactic sugar and compiler replaces each using block with equivalent try/finall block. The only issue is that the compiler does not wrap the constructor within the try block. Your code after compilation would have following conversion in the IL.

            //Declare object x of type SomeClass.
            SomeClass x;
    
            //Instantiate the object by calling the constructor.
            x = new SomeClass("c:/temp/test.txt");
    
            try
            {
                //Do some work on x.
            }
            finally
            {
                if(x != null)
                    x.Dispose();
            }
    

    As you can see from the code, the object x will not be instantiated in case when the constructor throws an exception and the control will not move further from the point of exception raise if not handled.

    I have just posted a blog-post on my blog on this subject last night.

    I’m just now wondering why C#
    designers did not wrap object
    construction within the try block
    which according to me should have been
    done.

    EDIT

    I think I found the answer why C# does not wrap object construction into try block generated in place of the using block.

    The reason is simple. If you wrap both declaration and instantiation within the try block then the object would be out of scope for the proceeding finally block and the code will not compile at because, for finally block the object hardly exists. If you only wrap the construction in the try block and keep declaration before the try block, even in that case the it will not compile since it finds you're trying to use an assigned variable.

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

Sidebar

Related Questions

Please see my example code: var testObject = new SomeClass(); using (testObject) { //At
Is it legal? class SomeClass { public: static void f(); }; using SomeClass::f; Edit:
Using the Redis info command, I am able to get all the stats of
I try for some time to get things done using MEF but now, I
I have a class like this public SomeClass { private List<string> _strings = new
I'm using reflection to iterate over all the members of a given type. This
#include <iostream> class SomeClass { public: int *SomeNumber; SomeClass() { SomeNumber = new int;
i'm new to protocol buffers and I am using protobuf-net for VS2010. from what
I would like to expand the functionality of some class using class_eval. I would
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain

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.