Why is this an issue?
The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the object is destroyed. The
method is protected and therefore is accessible only through this class or through a derived class. When you override Finalize
, you
should never throw an exception in it, as you are risking having your application terminated without a graceful cleanup.
If Finalize
or an override of Finalize
throws an exception and the runtime is not hosted by an application that overrides
the default policy and the behavior of the UnhandledExceptionEventHandler
, then the runtime terminates the process immediately without
graceful cleanup (finally
blocks and Finalizer
methods are not executed).
The rule reports on throw statements used in finalizers.
Noncompliant code example
Class MyClass
Protected Overrides Sub Finalize()
Throw New NotImplementedException() ' Noncompliant
End Sub
End Class
Compliant solution
Class MyClass
Protected Overrides Sub Finalize()
End Sub
End Class
Resources
Documentation