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 6072573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:10:30+00:00 2026-05-23T10:10:30+00:00

I have void foo1() { using(…){…} } void foo2() { using(…){…} } void foo3()

  • 0

I have

void foo1()
{
   using(...){...}
}

void foo2()
{
   using(...){...}
}

void foo3()
{
   using(...){...}
}

and I have

void foo()
{
    ...
    backgroundWorker.DoWork += (s, ev) =>
                                           {
                                               try
                                               {
                                                   foo1();
                                                   foo2();
                                                   foo3();
                                               }
                                               catch (Exception ex)
                                               {
                                                   // log ex
                                               }
                                           };
      ...    
}

and I just read that using blocks swallow exceptions. It there an elegant way to handle exceptions from foo1(), foo2() and foo3() in foo(). I don’t want to have a try/catch inside of each using block in the methods. I did stumble into this post where an extension method is suggested but I’m just checking to see if there is anything better.

FYI, Network disconnection causes the logic inside the using block to throw an exception and that’s what I’m trying to handle in one common place.

Thanks,

  • 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-23T10:10:30+00:00Added an answer on May 23, 2026 at 10:10 am

    I think I understand the confusion. Here’s some pseudo-code (it may actually execute?) explaining your scenario more simply:

    public class Foo
    {
        public void DoStuff()
        {
            using (var x = new Thing())
            {
                throw new ApplicationException("This code breaks");
            }
        }
    
        private class Thing : IDisposable
        {
            public override Dispose()
            {
                throw new ApplicationException("Help, I can't dispose!");
            }
        }
    }
    

    This code can be thought of as the same as this code:

    public class Foo
    {
        public void DoStuff()
        {
            var x = new Thing();
            try
            {
                throw new ApplicationException("This code breaks");
                x.Dispose();
            }
            catch (Exception err)
            {
                x.Dispose();
                rethrow;
            }
        }
    
        private class Thing : IDisposable
        {
            public override Dispose()
            {
                throw new ApplicationException("Help, I can't dispose!");
            }
        }
    }
    

    By using a using block, you are essentially saying, “No matter what you do, execute Dispose() on this object before moving on.” However, the using block doesn’t gracefully handle the case when Dispose() fails. Because of this, it never gets around to throwing the inside exception because there was another exception that pre-empted it, even if it occurred afterwards.

    Does this make sense? Did I even answer your question? I’m not sure if you’re looking for help understanding this or what.

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

Sidebar

Related Questions

Say I have: void Render(void(*Call)()) { D3dDevice->BeginScene(); Call(); D3dDevice->EndScene(); D3dDevice->Present(0,0,0,0); } This is fine
I have a function called: void initializeJSP(string Experiment) And in my MyJSP.h file I
i have a function like void printMe (void *i) { printf(%d, i); } where
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
I have the following piece of code pattern: void M1(string s, string v) {
I have the following intentionally trivial function: void ReplaceSome(ref string text) { StringBuilder sb
I have the following code: private static void WriteStartupInfo() { Settings settings = Settings.Default;
I have a C++ API prototype void Func(int& size); How can I translate it
I have an interface method public void Execute(ICommand command); which needs to pass known
I have the following C++ method : __declspec(dllexport) void __stdcall getDoubles(int *count, double **values);

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.