The using
block is shorthand for try/catch/finally
I believe. In my code I have been putting a try/catch
block inside the using
block, so that I can catch and log exceptions using my own logger.
I have recently been wondering if the try
should be on the outside, thus encapsulating the using
block, or not.
To my mind I have previously been worried that if an exception is thrown then the using
block will not Dispose()
of my resource because it has jumped out of the block and into the catch handler. But I may be wrong.
Could somebody please clarify which is the correct way to use both using
and try/catch
together?
public HttpResponseMessage GetData(string x, int y)
{
using (var client = new HttpClient())
{
try
{
// do stuff
return response.Result;
}
catch (Exception ex)
{
// Something has gone badly wrong so we'll need to throw
// Log the info
throw;
}
}
}
Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.
Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.
A try block is the block of code (contains a set of statements) in which exceptions can occur; it's used to enclose the code that might throw an exception. The try block is always followed by a catch block, which handles the exception that occurs in the associated try block.
Java try block is used to enclose the code that might throw an exception. It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.
using
block is used to ensure the disposal (changed from ambiguous "immediate disposal" term to disposal as suggested by Mr. James) of the objects declared in the using
statement as soon as the code is out of the using
block. It is not exactly the shorthand for try/catch/finally
. (Note that the compiler interprets it as try-finally however)
In your example
using (var client = new HttpClient())
{
} //client will be disposed at any time after this line
Depends on the case, you may consider of putting try-catch
block inside or outside of using
block.
For example, if you do not need to use the item in the using
declaration multiple times (edit: what I mean by this is if you need the item both in the try
and in the catch
block - thanks for input by Mr. Cody Gray) - that is, you only need it in try
, I would suggest to use using
block inside of the try
block.
Translated to your case, it depends on whether var client
is intended to be used both for the try
and the catch
blocks. If it is, the using
should be outside of the try-catch
block. Else, it should be inside of the try
block.
In general, as a rule of thumb, if the item is used both in the try
and catch
block, then declare using
only once outside of the try-catch
It really depends on what resource it is you are worried about being disposed - if you are referring to client
you should be fine. I'll explain why...
To my mind I have previously been worried that if an exception is thrown then the using block will not Dispose() of my resource because it has jumped out of the block and into the catch handler. But I may be wrong.
That's not how it works, the compiler will likely rewrite your code (probably more efficiently) like so:
try
{
try
{
// do stuff
return response.Result;
}
catch (Exception ex)
{
// Something has gone badly wrong so we'll need to throw
// Log the info
throw;
}
}
finally
{
if (client != null)
client.Dispose();
}
Under normal circumstances a finally
block will always execute which means, even if you catch/rethrow an exception inside the using
, your Dispose
call will be triggered.
So from a technical point of view, inside vs outside doesn't really matter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With