简体   繁体   中英

Try-Catch before Using or Vice Versa?

its been my practice to have the try-catch block inside of a using block rather than its vice versa. However, recently, i have been told that it wasn't the right way to go. As they've said the Database Connection does not close properly at this approach.

Below is an example block of how i implement it so:

using(var dbConn = new MySqlConnection(dbConnStr)) {
    using(var dbTrans = dbConn.BeginTransaction()) {
       try {
           ...
           ...
           ...

           return some_results_here;
        }
        catch(Exception Ex) {
            throw;
        }
    }
}

Accordingly, i was told that neither the dbConn nor the dbTrans automatically disposes (contrary to my belief as they are both disposable and in a using statement) and the connection is left open. Now i am confused and frustrated if it really is the case so. As this has been my practice on almost all the projects i have been working with and i have not encountered such issues with.

Please clear me up with this myth as so. Thank you all.

It doesn't matter if you put a try...catch before or after the using , since using is just a shorthand for a try...finally with a call to dispose.

Using is similar to this:

IDisposable d = (the assignment);

try
{
}
finally
{
    if (d != null)
        d.Dispose();
}

Besides some edge cases, in both cases your using instances will get disposed.

Patrick's answer perfectly clears up the question, however as additional support, directly from MSDN ...

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

You can also easily find a discussion on this: Here . In summary, it will dispose.

To answer your question specifically on the assertion that the throw inside the using block stops further progression of code getting run, (therefore the resources in your using blocks never get disposed)...

According to the Microsoft Docs

"The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. "

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

Note: I cannot be 100% certain this applies to every version of the .NET framework. If in doubt, I would suggest testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM