[Mono-list] using Statement in c# and garbage collection.

Jonathan Gilbert 2a5gjx302@sneakemail.com
Wed, 13 Oct 2004 01:06:13 -0400


At 09:15 AM 13/10/2004 +0530, you wrote: 
> Hi,
> 
>             We know that using statement like the one below would 
> 
> using (MyResource myObject = new MyClass())
[snip]
> gets translated to, 
> 
> MyClass myObject= new MyClass();
> 
> try
[snip]
> finally
> {
[snip]
> }
> 
> The problem I am having is I havent been able to figure out what If
> someone did
>
> using (MyResource myObject = new MyClass())
> {
[snip]
> --   throw SomeException( Exception thrown.;
>                                // assume an exception is thrown explicity.
> }
>
> What would happen now? Would the myObject get disposed ? Or would it
> stay ? What does the IL code look like in this situation ?? 

Exceptions do not bypass 'finally' blocks, and in fact if the code in the
'finally' block itself throws an exception, the previous exception
completely disappears.

Jonathan