There are definitely four options here:
Abandon
using
and just have atry
/catch
/finally
:var requestStream = request.GetRequestStream(); try { byte[] data = Encoding.UTF8.GetBytes(xmlData); requestStream.Write(data, 0, data.Length); } catch (IOException e) // Or whatever { // Whatever you want } finally { requestStream.Dispose(); }
Put just a
try
/catch
block inside theusing
block:using (var requestStream = request.GetRequestStream()) { try { byte[] data = Encoding.UTF8.GetBytes(xmlData); requestStream.Write(data, 0, data.Length); } catch (IOException e) // Or whatever { // Whatever you want } }
The same as the second option, but with the
try
/catch
outside theusing
block instead of inside. I don't think it makes much difference which you use, to be honest.Catch the exception at a higher level anyway, e.g. in the calling method. Often this is a better approach, as you may well want to handle one type of exception from multiple "small" operations in the same way from the perspective of the "larger" operation.
Personally I generally favour the second approach or the fourth. It's a consistent change: when I want to catch an exception, I introduce a try
/catch
block. The fact that it happens to be inside a using
statement is neither here nor there. It ends up with more nesting, but I find it happens rarely enough that that's not a problem.