Exception Handling and Garbage Collection

  • Thread starter Thread starter J. Marcelo Barbieri
  • Start date Start date
J

J. Marcelo Barbieri

a.. In Java, GC.Collect schedules the collection. As far as I know, in
..NET, it runs immediately after calling the method. Is that right?
b.. Should I use exception handling (try-catch) to avoid inconsistencies
that can occur in the Dispose method? For example, a connection is closed,
and then an error occurs and GC.SuppressFinalize is not executed. When GC
runs, it tries to close the connection again by calling the Dispose method
from Finalize.
c.. Is there a CATCH WHEN in C# to catch errors raised by a COM component
(err.raise)?
thanks again.
 
Hi Marcelo,

a) GC.Collect requests that a garbage collection is executed, but that's not
guaranteed. For example, if the GC is in the middle of a collection when
GC.Collect is called, it won't be executed again. Also, calling GC.Collect
is normally a bad practice, because the GC has a much better understanding
of the local environment.

b) Yes, I use Try...Catch to handle problems within my Dispose methods (and
also in my Finalize methods).

c) No, CATCH WHEN... is a trick reserved for VB .NET.

HTH,

Mark
--
Author of "Comprehensive VB .NET Debugging"
http://www.apress.com/book/bookDisplay.html?bID=128


a.. In Java, GC.Collect schedules the collection. As far as I know, in
..NET, it runs immediately after calling the method. Is that right?
b.. Should I use exception handling (try-catch) to avoid inconsistencies
that can occur in the Dispose method? For example, a connection is closed,
and then an error occurs and GC.SuppressFinalize is not executed. When GC
runs, it tries to close the connection again by calling the Dispose method
from Finalize.
c.. Is there a CATCH WHEN in C# to catch errors raised by a COM component
(err.raise)?
thanks again.
 
Back
Top