Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 144641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:17:08+00:00 2026-05-11T08:17:08+00:00

For the equivalent mechanism in C++ (the destructor), the advice is that it should

  • 0

For the equivalent mechanism in C++ (the destructor), the advice is that it should usually not throw any exceptions. This is mainly because by doing so you might terminate your process, which is only very rarely a good strategy.

In the equivalent scenario in .NET …

  1. A first exception is thrown
  2. A finally block is executed as a result of the first exception
  3. The finally block calls a Dispose() method
  4. The Dispose() method throws a second exception

… your process does not terminate immediately. However, you lose information because .NET unceremoneously replaces the first exception with the second one. A catch block somewhere up the call stack will therefore never see the first exception. However, one is usually more interested in the first exception because that normally gives better clues as to why things started to go wrong.

Since .NET lacks a mechanism to detect whether code is being executed while an exception is pending, it seems there are really only two choices how IDisposable can be implemented:

  • Always swallow all exceptions that occur inside Dispose(). Not good as you might also end up swallowing OutOfMemoryException, ExecutionEngineException, etc. which I’d usually rather let tear down the process when they occur without another exception already pending.
  • Let all exceptions propagate out of Dispose(). Not good as you might lose information about the root cause of a problem, see above.

So, which is the lesser of the two evils? Is there a better way?

EDIT: To clarify, I’m not talking about actively throwing exceptions from Dispose() or not, I’m talking about letting exceptions thrown by methods called by Dispose() propagate out of Dispose() or not, for example:

using System; using System.Net.Sockets;  public sealed class NntpClient : IDisposable {     private TcpClient tcpClient;      public NntpClient(string hostname, int port)     {         this.tcpClient = new TcpClient(hostname, port);     }      public void Dispose()     {         // Should we implement like this or leave away the try-catch?         try         {             this.tcpClient.Close(); // Let's assume that this might throw         }         catch         {         }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:17:09+00:00Added an answer on May 11, 2026 at 8:17 am

    I would argue that swallowing is the lesser of the two evils in this scenario, as it is better to raise the original Exception – caveat: unless, perhaps the failure to cleanly dispose is itself pretty darned critical (perhaps if a TransactionScope couldn’t dispose, since that could indicate a rollback failure).

    See here for more thoughts on this – including a wrapper/extension method idea:

    using(var foo = GetDodgyDisposableObject().Wrap()) {    foo.BaseObject.SomeMethod();    foo.BaseObject.SomeOtherMethod(); // etc } // now exits properly even if Dispose() throws 

    Of course, you could also do some oddity where you re-throw a composite exception with both the original and second (Dispose()) exception – but think: you could have multiple using blocks… it would quickly become unmanageable. In reality, the original exception is the interesting one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an equivalent mechanism to the ItemsControl.ItemTemplate that works with a Grid ?
I'm looking for exception handling mechanism in shell script. Is there any try,catch equivalent
Just read of ppk's web site that IE's mechanism of registering events does not
Is there a pexpect equivalent for java? I know that I could replicate the
Does PowerShell have an equivalent to this command construct from sh (and derivatives): $
I'm wondering if there is an equivalent to Qt's signal/slot mechanism for Perl. I
I'm trying to write an equivalent of Rails data model evolution/rollback mechanism using Spring
I have a site that treats / and %2F in the path portion (not
In Java, I will occasionally throw an AssertionError directly, to assert that a particular
Is there a Java equivalent to PHP's mysql_real_escape_string() ? This is to escape SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.