【发布时间】:2020-08-18 06:02:33
【问题描述】:
我正在使用以下一般包装功能:
public static class ErrorHandling
{
public static void TryCatchErrors<TLogger>(ILogger<TLogger> logger, Action action, string? customMsg = null) => TryCatchErrors<TLogger, object>(logger, () => { action.Invoke(); return 0; }, customMsg);
public static TOut TryCatchErrors<TLogger, TOut>(ILogger<TLogger> logger, Func<TOut> action, string? customMsg = null)
{
try
{
if (logger == null) { throw new ArgumentNullException($"Got null for logger", $"Expected type ILogger<{typeof(TLogger).AssemblyQualifiedName}>!"); }
if (action == null) { throw new ArgumentNullException($"Got null for action", $"Expected type Func<{typeof(TOut).AssemblyQualifiedName}>!"); }
return action.Invoke();
}
catch (Exception e)
{
logger.LogError(e, customMsg ?? e.Message);
}
}
}
当我执行以下我的预期用途示例时,我的包装函数无法捕获错误:
public static async Task DeleteRecords<TLog>(ILogger<TLog> _logger) =>
await ErrorHandling.TryCatchErrors(_logger, async () =>
{
// Other functionality that might throw an unexpected error etc.
throw new Exception();
});
虽然我在这里遗漏一些愚蠢的东西的可能性绝对是真实的,但我的印象是,这与一些我不知道并且一直在努力弄清楚自己的较低级别的问题或担忧有关。
提前致谢!
【问题讨论】:
-
我认为第一个方法不会编译,你正在捕捉异常,但你必须返回一些东西。
-
为什么你会在那里捕捉到异常,即使你无法解决它。 (例如返回任何有用的东西)当你能够解决它们时捕获异常。
标签: c# .net lambda error-handling wrapper