捕獲異步空方法引發(fā)的異常。使用Microsoft for.NET的異步CTP,是否有可能捕獲調(diào)用方法中的異步方法引發(fā)的異常?public async void Foo(){
var x = await DoSomethingAsync();
/* Handle the result, but sometimes an exception might be thrown.
For example, DoSomethingAsync gets data from the network
and the data is invalid... a ProtocolException might be thrown. */}public void DoFoo(){
try
{
Foo();
}
catch (ProtocolException ex)
{
/* The exception will never be caught.
Instead when in debug mode, VS2010 will warn and continue.
The deployed the app will simply crash. */
}}因此,基本上,我希望異步代碼中的異常出現(xiàn)在我的調(diào)用代碼中,如果這是可能的話。
3 回答

明月笑刀無情
TA貢獻1828條經(jīng)驗 獲得超4個贊
Foo()
Task
DoFoo()
await Foo()
public async Task Foo() { var x = await DoSomethingThatThrows();}public async void DoFoo() { try { await Foo(); } catch (ProtocolException ex) { // This will catch exceptions from DoSomethingThatThrows }}
- 3 回答
- 0 關(guān)注
- 502 瀏覽
添加回答
舉報
0/150
提交
取消