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,
I think I understand the confusion. Here’s some pseudo-code (it may actually execute?) explaining your scenario more simply:
This code can be thought of as the same as this code:
By using a
usingblock, you are essentially saying, “No matter what you do, executeDispose()on this object before moving on.” However, theusingblock doesn’t gracefully handle the case whenDispose()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.