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

  • Home
  • SEARCH
  • 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 6696053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:17:45+00:00 2026-05-26T06:17:45+00:00

I instantiate a disposable object (in my case a FileStream) in my constructor and

  • 0

I instantiate a disposable object (in my case a FileStream) in my constructor and need to do some work on it. However, this might throw any number of different exceptions. Now I don’t even want to mess with these exceptions too much and want to allow them to propagate up to the caller. However, I need to dispose the object first. Now what’s the best way to do that? Right now I can only think of something like that:

IDisposable disposableObject = InstantiateDisposable();
bool error = false;
try
{
    DoWork(disposableObject);
}
catch (ReallyBadException e)
{
    error = true;
    throw new EvenWorseException("some message", e);
}
catch (Exception)
{
    error = true;
    throw;
}
finally
{
    if (error) disposableObject.Dispose();
}

Is that even correct or will the Dispose() be skipped in some special cases? Is there an easier way to do that? It gets a bit cumbersome, if you need to catch a bunch of different exceptions seperately for whatever reason and you always have to copy & paste that error = true; bit.

Edit:
Just to clarify: I only need to dispose the object in case of DoWork() failing / throwing an exception. If this method succeeds, I don’t want to dispose the object just yet, as there’ll be more work to do with it later on.

  • 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. Editorial Team
    Editorial Team
    2026-05-26T06:17:45+00:00Added an answer on May 26, 2026 at 6:17 am

    Why not invert the logic?

    IDisposable disposableObject = InstantiateDisposable();
    bool error = true;
    try
    {
        DoWork(disposableObject);
        error = false; // if it gets to this line, no exception was thrown
    }
    catch (ReallyBadException e)
    {        
        throw new EvenWorseException("some message", e);
    }    
    finally
    {
        if (error) disposableObject.Dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I instantiate an object in Javascript like this: var myObj = new
I need to instantiate some ASP LinkButtons onto an ASP.NET MVC view page. I've
I want to instantiate a Vector.<Number> with two values of 1.0 You might think
I am trying to instantiate a PDO object like this: $this->pdo['pdo'] = new PDO('mysql:host=127.0.0.1;dbname=mydb;charset=UTF-8',
I instantiate the HttpWebRequest object: HttpWebRequest httpWebRequest = WebRequest.Create(http://game.stop.com/webservice/services/gameup) as HttpWebRequest; When I post
Is it ok to instantiate an object in a View before passing it to
How can I instantiate a class by throwing in a variable name? Consider this
Can I avoid instantiate a Db object inside of Names object to access it
When I want to instantiate a class in C++ I usually go this way
I need to instantiate a new view on a command Imagine I have a

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.