If an exceptional condition arises in Dispose
, it is usually better to have Dispose
throw the exception than complete silently (whether it's better to stifle the exception in any particular situation will depend upon whether an exception is already pending and there is as yet alas no mechanism via which Dispose
can know when that's the case). Because there is no good way to handle an exception within Dispose
, it's often a good idea to ensure when possible that any actions which could go wrong if done within a Dispose
will be done before the Dispose
is invoked.
If Dispose
were the only method of cleanup in the normal case, then having it stifle exceptions would pose a significant risk that problems could occur but go undetected. Having a class support both Close
and using
, and having clients call Close
in the main-line case, will allow such risk to be reduced (if an exception is pending, Dispose
will get called without Close
, and thus any exceptions on cleanup will get stifled but code will know that something went wrong somewhere because of the pending exception; if no exception occurs before Close
, the fact that it's not being called in a Dispose
cleanup context will mean that it can assume no exception is pending, and it may thus safely throw one of its own).
There's not much consistency in the exception-handling practices of Dispose
and Close
, but I would recommend calling Close
on principle. Depending upon how Dispose
and Close
are implemented, explicitly calling Close
before the implicit Dispose
may or may not be helpful, but it should be at worst harmless. Given the possibility of its being helpful (if not in the present version of a class, perhaps in a future version) I would suggest it as a general habit.