11

Which one is better in structure?

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (Foo f = new Foo())
            {
                //some commands that potentially produce exceptions.
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

or...

class Program
{
    static void Main(string[] args)
    {

        using (Foo f = new Foo())
        {
            try
            {
                //some commands that potentially produce exceptions.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}
2
  • 3
    stackoverflow.com/questions/911626/… Commented Aug 3, 2010 at 10:27
  • If you intend to just log any exception thrown from Main, go with Option1. If you need to access f in the exception handler before it gets disposed, you need to use Option2 - else f would go out of scope. - Dupe, voting to close. Commented Aug 3, 2010 at 10:28

6 Answers 6

7

Either is fine, depending on what you are going to do in the catch. If you need to use f in your catch then it needs to be within the using statement. However in your example there is no difference.

EDIT: As pointed out elsewhere it also depends on whether you are trying to catch just exceptions generated in the block following the using or including the object creation in the using statement. If it is in the block following the using then it is as I described. If you want to catch exceptions generated by Foo f = new Foo() then you need to use the first method.

Sign up to request clarification or add additional context in comments.

2 Comments

what if i go with opt 2 and exception get throw form constructor
@June R. I have added some edits, thanks. The problem with generic questions is there is always exceptions to the rule. Without knowing the actual usage I don't think it is possible to say one is more correct than the other. The real answer is it depends on what you are doing.
2

I don't think it matters much, performance-wise. There is a slight difference though; in the second example, f is still available inside the exception handler, while in the first, it has gone out of scope. Conversely, in the first example, exceptions in the Foo constructor as well as its Dispose method will be caught, while in the second, they won't.

Either may or may not be what you want.

1 Comment

what if i go with opt 2 and exception get throw form constructor
1

Check this post to understand better : http://www.ruchitsurati.net/index.php/2010/07/28/understanding-%E2%80%98using%E2%80%99-block-in-c/

Also read answers of this question : Catching exceptions thrown in the constructor of the target object of a Using block

the first one is bettwer one

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (Foo f = new Foo())
            {
                //some commands that potentially produce exceptions.
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

because if you see the IL code of this try and catch block not wrap the inialization of the object.

Comments

1

The first is better, because it will catch any exceptions thrown during the dispose process. Of course, you shouldn't throw any exceptions when disposing, but stuff happens.

1 Comment

Similarly, it will also catch exceptions thrown in Foo's constructor, which the second form won't.
1

First one is the better one .if any exception comes it will catch.

 try
    {
        using (Foo f = new Foo())
        {
            //some commands that potentially produce exceptions.
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

The concept of using is it will dispose the object created in the using.i.e it automatically calls the IDispose method.Based on the requirement use the using.

Comments

1

Using is just

Foo f = null;
try
{
   f = new Foo();
}
finally
{
   if (f is IDisposable)
       f.Dispose();
}

Seeing that you can achive catching exceptions like this:

Foo f = null;
try
{
   f = new Foo();
}
catch (Exception ex)
{
    // handle exception
}
finally
{
   if (f is IDisposable)
       f.Dispose();
}

1 Comment

When f.Dispose() potentially emits other exceptions, it must be enclosed by try-catch also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.