A developer has written the following code below and I am trying to find a better way to refactor it:
try
{
using (var client = new WebClient())
{
var proxy = new WebProxy(address, portNo);
proxy.BypassProxyOnLocal = false;
proxy.UseDefaultCredentials = true;
client.UseDefaultCredentials = true;
result = client.DownloadString(httpURL);
}
}
catch (Exception ex)
{
log.Error("blah blah", ex);
try
{
SendNotification
}
catch (Exception emailEx)
{
log.Error("blahblah " + emailEx);
}
}
Is the using clause required to be inside a try/catch block, considering it itself is using try/finally? And then if an exception is thrown inside the using, how do I handle it?
Is there a better way to avoid the nested try/catch when sending a notification?
SendNotificationcode to its own method.public void SendNotificationCatchExceptions(), 2) Move your nested try/catch into that method. 3) CallSendNotificationCatchExceptions()from your maincatchblock.SendNotificationwill require the outermost exception, of course.usinganyway (nor close it manually everytime).