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,
usingblock shouldn't have anything to do with it. However, I'm making some assumptions here as well. Ausingblock is just syntactic sugar for creating an IDisposable variable, running code, then calling Dispose() on that object at the end if I recall correctly.usingto atry-finallyblock where thefinallyblock callsDispose()but there is no catch block so any exceptions that may be thrown are lost.