1

Topic: [.NET2] Exception + Dispose

Where disappears Exception "Error in Process" in the resulted code?




using System; 

namespace ConsoleApplication5 
{ 
    public class A: IDisposable 
    { 
        public void Process () 
        { 
            Console. WriteLine ("Process - begin"); 
            throw new NotImplementedException ("Error in Process"); 
        }

        public void Dispose () 
        { 
            Console. WriteLine ("Dispose - begin"); 
            throw new NotImplementedException ("Error in Dispose"); 
        }
    }

    class Program 
    { 
        static void Main (string [] args) 
        { 
            Console. WriteLine ("Main1 - Begin"); 

            try 
            { 
                using (A a = new A ()) 
                { 
                    a. Process (); 
                }
            }
            catch (Exception ex) 
            { 
                Console. WriteLine ("Exception:" + ex. ToString ()); 
                Console. WriteLine ("InnerException:" + (ex. InnerException! = null? ex. InnerException. ToString (): "<null>")); 
            }

            Console. WriteLine ("Main1 - End"); 

            Console. ReadLine (); 
        }
    }
}

2

Re: [.NET2] Exception + Dispose

Hello, the Anonymous author, you wrote:

Where disappears Exception "Error in Process" in the resulted code?


It is replaced with that happened in Dispose (). If it is not clear, rewrite using through try/finally and walk by a debugger - all will be clear.


Generally, to throw  from Dispose () - vicious practice. And, in particular, just because of the resulted example.

3

Re: [.NET2] Exception + Dispose

Hello, _FRED _, you wrote:

_FR> Hello, the Anonymous author, you wrote:

> Where disappears Exception "Error in Process" in the resulted code?

_FR> that happened in Dispose Is replaced (). If it is not clear, rewrite using through try/finally and walk by a debugger  all will be clear.

_FR> Generally to throw exceptions from Dispose ()  vicious practice. And, in particular, just because of the resulted example.


Thanks.


And how it to catch using using?

Unless Exception can irrevocably disappear without being processed?

Clearly that if through try/finally - it is possible to place direct call Dispose in the try/catch.


At me in Dispose - closing of "resources" during which time too can happen Exception.

4

Re: [.NET2] Exception + Dispose

Hello, the Anonymous author, you wrote:

>> Where disappears Exception "Error in Process" in the resulted code?
_FR>> that happened in Dispose Is replaced (). If it is not clear, rewrite using through try/finally and walk by a debugger  all will be clear.
_FR>> Generally to throw exceptions from Dispose ()  vicious practice. And, in particular, just because of the resulted example.

And how it to catch using using?


To write try/catch inside using or not to throw in Dispose ().

Unless Exception can irrevocably disappear without being processed?


Can. Throwing from Dispose () is .

Clearly that if through try/finally  it is possible to place direct call Dispose in the try/catch.
At me in Dispose  closing of "resources" during which time too can happen Exception.


Inside Dispose () to do try/catch.


What there for resources such also what there for clearing which can end with an exception?

5

Re: [.NET2] Exception + Dispose

Hello, _FRED _, you wrote:

_FR> to Write try/catch inside using or not to throw in Dispose ().

> Unless Exception can irrevocably disappear without being processed?

_FR> Can. Throwing from Dispose () is .

> Clearly that if through try/finally  it is possible to place direct call Dispose in the try/catch.
> At me in Dispose  closing of "resources" during which time too can happen Exception.

_FR> Inside Dispose () to do try/catch.

_FR> That there for resources such also what there for clearing which can end with an exception?



There is a class for generating of documents in which it is defined Dispose.


There is one of subclasses - generating of documents in MS Excel.


In Dispose - I close Excel and I release a COM object since if it not to make Excel and will hang in processes

For operation with Excel - late binding + as the server is used the remote machine is used.

All operations with COM objects are implemented with usage Dispose since if to forget and to release the link on COM object Excel will hang in processes.


If at the moment of document generating there is an error, for example to kill Excel in Dispose - at closing Excel - there will be an error.

But the error can be and another and then Excel - it is necessary to close.

6

Re: [.NET2] Exception + Dispose

Hello, the Anonymous author, you wrote:

_FR>> That there for resources such also what there for clearing which can end with an exception?


There is a class for generating of documents in which it is defined Dispose.
There is one of subclasses  generating of documents in MS Excel.

In Dispose  I close Excel and I release a COM object since if it not to make Excel and will hang in processes
For operation with Excel  late binding + as the server is used the remote machine is used.
All operations with COM objects are implemented with usage Dispose since if to forget and to release the link on COM object Excel will hang in processes.

If at the moment of document generating there is an error, for example to kill Excel in Dispose  at closing Excel  there will be an error.
But the error can be and another and then Excel  it is necessary to close.


And in Dispose () there is no possibility to define, whether in  it a state?
lol

Then close Excel explicitly (that is make method Close () and explicitly cause it) it is valid operation which can be completed unsuccessfully and the external world should learn about it.

7

Re: [.NET2] Exception + Dispose

Hello, the Anonymous author, you wrote:

Unless Exception can irrevocably disappear without being processed?


Unfortunately, can. C# Language Specification, 8.10 The try statement:



If an exception is thrown during execution of a finally block, and is not caught within the same finally block, the exception is propagated to the next enclosing try statement. If another exception was in the process of being propagated,
that exception is lost.