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

Nathan Neitzke Nathan Neitzke <nightski@gmail.com>
Tue, 12 Oct 2004 23:50:10 -0400


Sarvesh,
Yes, the finally block still gets executed since the exception is
thrown within the try block.  However, since there is no catch block
the exception will not be caught (unless if it is at a higher level)
therefore causing an exception message box being shown to the user.

The Try..Catch..Finally blocks work like this.  If an exception is
thrown in the try block then execution moves to the catch block (if
there is one).  The developer can then handle the exception here. 
However, let's say another exception is caused within the catch block.
 The finally block in this case is still guaranteed to execute.  That
is the whole point of the finally block - no matter what (even if an
exception is not thrown) - the finally block will execute.

Take care,
Nathan


On Wed, 13 Oct 2004 09:15:14 +0530, sarvesh <sarvesh@hathway.com> wrote:
> 
> 
> 
> Hi,
> 
>             We know that using statement like the one below would 
> 
>  
> 
> using (MyResource myObject = new MyClass())
> 
> {
> 
>     myObject.DoSomething();
> 
>  
> 
> }
> 
> gets translated to, 
> 
>  
> 
>     
> 
> MyClass myObject= new MyClass();
> 
> try
> 
> {
> 
>     myObject.DoSomething();
> 
> }
> 
> finally
> 
> {
> 
>     // Check for a null resource.
> 
>     if (myObject!= null)
> 
>         // Call the object's Dispose method.
> 
>         ((IDisposable)myObject).Dispose();
> 
> }
> 
>  
> 
>  
> 
> The problem I am having is I haven't been able to figure out what If someone
> did
> 
>  
> 
> using (MyResource myObject = new MyClass())
> 
> {
> 
>     myObject.DoSomething();
> 
> --   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 ?? 
> 
>  
> 
> Regards, 
> 
> sarvesh