【发布时间】:2014-10-07 21:26:14
【问题描述】:
我正在尝试编写一个方法来尝试执行某个操作,但会吞下引发的任何异常。
我的第一次尝试如下:
public static void SafeExecute(Action actionThatMayThrowException) {
try {
actionThatMayThrowException();
} catch {
// noop
}
}
使用同步操作调用时有效:
SafeExecute(() => {
throw new Exception();
});
但是当使用异步操作调用时失败:
SafeExecute(async () => {
await Task.FromResult(0);
throw new Exception();
});
是否可以编写一个处理这两种情况的方法?
【问题讨论】:
-
你使用的是哪个框架??
标签: c# .net exception-handling task-parallel-library async-await