第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

捕獲異步空方法引發(fā)的異常。

捕獲異步空方法引發(fā)的異常。

拉風(fēng)的咖菲貓 2019-07-22 19:00:12
捕獲異步空方法引發(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貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

未捕獲異常的原因是foo()方法有一個(gè)空返回類型,因此當(dāng)調(diào)用WAIT時(shí),它只是返回。由于DoFoo()沒有等待foo的完成,因此不能使用異常處理程序。

這打開了一個(gè)更簡(jiǎn)單的解決方案,如果您可以更改方法簽名-ALTERFoo()以便它返回類型Task然后DoFoo()能,會(huì),可以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
    }}


查看完整回答
反對(duì) 回復(fù) 2019-07-22
  • 3 回答
  • 0 關(guān)注
  • 367 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)