我在我的代码中有如下经常出现的try/catch模式。使用try/catch块来处理在调用orionProxy方法时抛出的任何异常。
async private void doGetContacts()
{
try {
currentContacts = await orionProxy.GetContacts (); // call method in orionProxy
ShowContacts (); // do something after task is complete
}
catch (Exception e) {
orionProxy.HandleException (e); // handle thrown exception
}
}
我想要写的内容是类似以下的东西。
async private void doGetContacts()
{
currentContacts = await orionProxy.CheckForException(orionProxy.GetContacts ());
ShowContacts (); // do something after task is complete but shouldn't run on exception
}
有什么建议吗?我尝试了各种形式的Actions/Tasks/Lambdas,但没有一个能够正确捕获orionProxy.CheckForException(?)中的异常,所以ShowContacts无法运行。