C# "Using" Syntax
Solution 1
using statements do not eat exceptions.
All "Using" does is scope your object to the using block, and automatically calls Dispose() on the object when it leaves the block.
There is a gotcha though, if a thread is forcefully aborted by an outside source, it is possible that Dispose will never be called.
Solution 2
When you see a using statement, think of this code:
StreadReader rdr = null;
try
{
rdr = File.OpenText("file.txt");
//do stuff
}
finally
{
if (rdr != null)
rdr.Dispose();
}
So the real answer is that it doesn't do anything with the exception thrown in the body of the using block. It doesn't handle it or rethrow it.
Solution 3
using
allows the exception to boil through. It acts like a try/finally, where the finally disposes the used object. Thus, it is only appropriate/useful for objects that implement IDisposable
.
Solution 4
It throws the exception, so either your containing method needs to handle it, or pass it up the stack.
try
{
using (
StreamReader rdr = File.OpenText("file.txt"))
{ //do stuff
}
}
catch (FileNotFoundException Ex)
{
// The file didn't exist
}
catch (AccessViolationException Ex)
{
// You don't have the permission to open this
}
catch (Exception Ex)
{
// Something happened!
}
Solution 5
Any exceptions that are thrown in the initialization expression of the using statement will propagate up the method scope and call stack as expected.
One thing to watch out for, though, is that if an exception occures in the initialization expression, then the Dispose() method will not be called on the expression variable. This is almost always the behavior that you would want, since you don't want to bother disposing an object that was not actually created. However, there could be an issue in complex circumstances. That is, if multiple initializations are buried inside the constructor and some succeed prior to the exception being thrown, then the Dispose call may not occur at that point. This is usually not a problem, though, since constructors are usually kept simple.
Gordon Thompson
I work in many languages such as C/Java/C#/Perl currently cutting my teeth on ASP.NET but I'm sure that'll change in 10 minutes time :) Edit : Have discovered JQuery and realised that I can do stuff in a day using JQuery and WebServices that took me a week to do in ASP.NET. Very happy :)
Updated on July 09, 2022Comments
-
Gordon Thompson almost 2 years
Does the using catch the exception or throw it? i.e.
using (StreamReader rdr = File.OpenText("file.txt")) { //do stuff }
If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it?
-
Brendan Enrick over 15 yearsI am fairly certain that your "gotcha" is not accurate. Since the StreamReader class implements IDisposable, the using statement will take care of the disposal of the object. Because the using statement acts like a finally block, it doesn't matter if you have an exception or return.
-
Uhall over 15 yearsAccording to msdn.microsoft.com/en-us/library/… , catch and finally statements are still executed. Since "using" compiles as a finally block, the stread reader in your example will be disposed.
-
Pino Gatto over 15 yearsI would concur with the previous two comments. In addition to @Uhall's comment above, the ThreadAbortException does not abort the thread, it just catches the exception that is raised as a result.
-
Peter Ramos over 15 yearsQuite right...it only happens if the thread is forcefully aborted, not when throwing the exception. Updating the answer.
-
Joe over 15 yearsThis is always the behavior you want. If multiple initializations are buried inside the constructor, it's up to the constructor to manage any necessary cleanup. No outside code could possibly do so, as the object won't actually be created.
-
Jeffrey L Whitledge over 15 years@Joe - I'm trying to sound smart here. Stop pointing out the flaws in my logic!