Exception in Destructor (C #)?

I have this class:

public class TempFileRef
    {
        public readonly string FilePath;

        public TempFileRef(string filePath)
        {
            FilePath = filePath;
        }

        ~TempFileRef()
        {
            File.Delete(FilePath);    //<== what happens if exception ?
        }
    }

      

Question:

What happens if there is an exception in the destructor?

1) will it break another finalization in the F-Queue?

2) I will wrap it up with Try

and Cache

- I will NEVER

know that an error has occurred

3) what

Should I be here?

change

MSDN template for it based on "if I **forget** to call the Dispose method - so the GC will do it eventually.... it is better later then never..."

. So my question is specifically about the exception in Finilize (destructor)

+3


source to share


4 answers


From MSDN :



Exceptions thrown during destructor execution, mention. If an exception is thrown during the execution of a destructor, and that the exception is not caught, then the execution of that destructor and the destructor of the base class (if any). If there is no base class (as in the case of the object type), or if it is not a destructor of the base class, then the exception is thrown.

+9


source


It really depends on the .NET framework

For example, in .NET 2 and .NET 4 , the application will terminate

If Finalize or the Finalize override throws an exception and the runtime is not a hosted application that overrides the default policy, the runtime terminates the process and does not activate the try blocks or finalizers. This behavior enforces the process if the finalizer cannot free or destroy resources.



Unlike .NET 1 , only this finalizer will be terminated and your application will continue running:

If Finalize or the Finalize override throws an exception, runtime ignores the exception, terminates this Finalize method, and continues the completion process.

What you are actually trying to do is implement the template IDisposable

, so this work should end with a finalizer instead, do it in a progmatically called Dispose

.

+13


source


Instead of deleting the file in the finalizer, consider implementing the interface IDisposable

.

Exceptions thrown during destructor execution, mention. If an exception is thrown during the execution of a destructor, and that the exception is not caught, then the execution of that destructor and the destructor of the base class (if any). If there is no base class (as in the case of the object type), or if it is not a destructor of the base class, then the exception is thrown.

http://msdn.microsoft.com/en-us/library/aa664609%28v=vs.71%29.aspx

+6


source


Don't use Destructor, don't C++

use an Dispose()

interface method IDisposbale

that you have to implement. Call Dispose () implicitly from your code, or use your class with using

like (say)

using(var tempRef = new TempFileRef())
{
    //do something here

} //Dispose will be called after this line.

      

EDIT

According to the documentation

If Finalize or the Finalize override throws an exception and the runtime is not hosted by an application that overrides the default policy, the runtime terminates the process and does not activate the try blocks or finalizers. This behavior enforces the process if the finalizer cannot free or destroy resources.

In other words: don't call something in the Finalizer that might fail. Use Dispose instead and make sure it was called.

Hope it helps.

0


source







All Articles