views:

151

answers:

8

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);
            }
        }

    }
}
+1  A: 

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 : http://stackoverflow.com/questions/3360860/catching-exceptions-thrown-in-the-constructor-of-the-target-object-of-a-using-blo/3360930#3360930

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.

Pranay Rana
+6  A: 

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.

btlog
i dont agree read the link pasted in my answer
Pranay Rana
what if i go with opt 2 and exception get throw form constructor
Pranay Rana
@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.
btlog
+2  A: 

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.

tdammers
i dont agree read the link pasted in my answer
Pranay Rana
what if i go with opt 2 and exception get throw form constructor
Pranay Rana
Edited to clarify.
tdammers
+1  A: 

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.

Will
yes that what i suggesting....
Pranay Rana
Similarly, it will also catch exceptions thrown in Foo's constructor, which the second form won't.
C.McAtackney
+1  A: 

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.

anishmarokey
+1  A: 

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();
}
schoetbi
When f.Dispose() potentially emits other exceptions, it must be enclosed by try-catch also.
xport
A: 

my answer is:

answer you find here

http://websitereckon.com

thanks

steve
-1 for spamming.
xport
wtffffffffffffffffff
Pranay Rana