В настоящее время я работаю с некоторым кодом, который используется в связи с клиентским сервером.Какие проблемы вы видите при обработке исключений для одного метода?
Я очень много (около 50) из следующих блоков try-catch
try
{
return GetLogFiles(date);
}
catch (TimeoutException ex)
{
this.GetLogger("GetAllLogs").Error("C is not answering!", ex);
}
catch (ConnectionInterruptedException ex)
{
this.GetLogger("GetAllLogs").Error("Connection interrupted", ex);
}
catch (ActionNotSupportedException ex)
{
this.GetLogger().Error("Software-version not comaptible!", ex);
throw new VersionNotCompatibleException();
}
catch (EndpointNotFoundException ex)
{
this.GetLogger().Error("Problem with network, connection to core is lost!", ex);
}
catch (CommunicationException ex)
{
this.GetLogger().Error("Not expected communication-exception was thrown:", ex);
}
Это делает много кода, который более или менее всегда то же самое.
Теперь я подумал о реорганизации всех блоков catch в метод и просто позвонил ему. Как
try
{
return GetLogFiles(date);
}
catch(Exception ex)
{
ExceptionHandling(string operation, Exception ex)
}
private void ExceptionHandling(string operation, Exception ex)
{
if (ex is TimeoutException)
{
this.GetLogger(operation).Error("C is not answering!", ex);
}
else if (ex is ConnectionInterruptedException)
{
this.GetLogger(operation).Error("Connection interrupted", ex);
}
else if (ex is CommunicationObjectFaultedException)
{
this.GetLogger(operation).Error("Core is not answering!", ex);
}
else if (ex is FaultException)
{
this.GetLogger(operation).Error("C is not answering!", ex);
}
else if (ex is ActionNotSupportedException)
{
this.GetLogger().Error("Software-version not comaptible!", ex);
throw new VersionNotCompatibleException();
}
else if (ex is EndpointNotFoundException)
{
this.GetLogger().Error("Problem with network, connection is lost!", ex);
}
else if (ex is CommunicationException)
{
this.GetLogger().Error("Not expected communication-exception was thrown:", ex);
}
else
{
this.GetLogger().Error("Unknown exception was thrown:", ex);
throw new Exception("Unknown exception occured during request handling", ex);
}
}
Ни один из codeparts не делают любое обычное обращение в случае исключения, то есть они получают вход и это все.
Какие проблемы вы видите при извлечении части исключения в свой собственный метод?
Единственная проблема, которую я вижу, это почему бы не использовать третью партию ошибки регистратора – Alex
@Alex Мы далеко от видеть это как проблема на данный момент - по крайней мере, я есть) –
Вы ничего не получаете от разделения обработки ошибок, как вы это делаете. На самом деле, ваш код еще длиннее ... –