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

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

如何確保操作不會(huì)使整個(gè)應(yīng)用程序崩潰?

如何確保操作不會(huì)使整個(gè)應(yīng)用程序崩潰?

C#
縹緲止盈 2023-09-16 15:01:56
我有一個(gè)應(yīng)用程序執(zhí)行一些額外的工作,例如清理舊日志、發(fā)送通知等。如果一項(xiàng)作業(yè)失敗,我不希望整個(gè)應(yīng)用程序停止工作并且不執(zhí)行剩下的作業(yè)。例如,await SendUsersBirthdayEmailsAsync(); // <-- if something fails while trying to send birthday emails here, I don't want the app to stop working and not clean logs and so on...await DeleteOutdatedLogsAsync();await SendSystemNotificationsAsync();你會(huì)推薦我搭配什么?
查看完整描述

6 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

try-catch對(duì)可能失敗的代碼的每個(gè)部分使用塊。

根據(jù)您的需要,使用try-catch-finally塊。


在每個(gè)catch塊上,根據(jù)需要記錄異常。我使用 Nlog 進(jìn)行日志記錄,因此我建議對(duì)此進(jìn)行研究。


try{

? ? //do work here

}

catch(Exception e){

? ? //log exception here

}

//optional

finally{

? ? //do optional needed work here

}

像這樣的東西:


public bool SendUsersBirthdayEmailsAsync(){

? ? try{

? ? ? ? SendMail();

? ? }

? ? catch(Exception e){

? ? ? ? LogException(e);

? ? }

? ? //optional

? ? finally{

? ? ? ? OptionalWork();

? ? }? ? ? ?

}

編輯:關(guān)于避免使用通用異常


您始終可以使用多個(gè)catch塊來為每種類型的異常定義不同的行為。當(dāng)您知道可能會(huì)出現(xiàn)什么樣的異常時(shí),這非常有用。

例子:


public bool SendUsersBirthdayEmailsAsync(){

? ? try{

? ? ? ? SendMail();

? ? }

? ? catch (ThreadAbortException tae)

? ? {

? ? ? ? LogException(tae);

? ? ? ? //do something specific

? ? }

? ? catch (ThreadInterruptedException tie)

? ? {

? ? ? ? LogException(tie);

? ? ? ? //do something specific

? ? }

? ? catch(Exception e){

? ? ? ? LogException(e);

? ? }

? ? //optional

? ? finally{

? ? ? ? OptionalWork();

? ? }? ? ? ?

}

編輯 2:異常處理的Microsoft 官方指南。

try/catch在可能生成異常的代碼周圍使用塊,您的代碼可以從該異常中恢復(fù)。在catch塊中,始終將異常從派生最多的到派生最少的排序。所有異常均源自Exception.?更多派生異常不會(huì)由catch前面有catch基本異常類子句的子句處理。當(dāng)您的代碼無法從異常中恢復(fù)時(shí),不要捕獲該異常。如果可能的話,啟用調(diào)用堆棧上方的方法進(jìn)行恢復(fù)。

清理用using語句或finally塊分配的資源。Preferusing語句在拋出異常時(shí)自動(dòng)清理資源。使用finally塊來清理未實(shí)現(xiàn)的資源IDisposable。finally即使拋出異常,子句中的代碼也幾乎總是會(huì)執(zhí)行。


查看完整回答
反對(duì) 回復(fù) 2023-09-16
?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

您需要在每個(gè)函數(shù)中使用 Try-Catch,如下所示。


try{

    //write your code

}

catch(Exception e){

    //your exception will be here

}

您可以為該異常生成一個(gè)日志文件。只需為您的日志文件做一件事。創(chuàng)建日志類和日志文件生成函數(shù)。在所有函數(shù)的 catch 區(qū)域中調(diào)用該函數(shù)。


讓您創(chuàng)建一個(gè)名為clsLog的 Log 類。和一個(gè)名為InsertLog(string exception, string functionname)的靜態(tài)函數(shù)。


在您的所有函數(shù)中使用此日志方法,如下所示。


public void insertcity()

{

 try

 {

   //Insert city programming

 }

 catch (exception ex)

 {

   clsLog.InsertLog(ex.Message,"insertCity");

   //do something else you want.

 }        

}

希望它會(huì)有所幫助。


查看完整回答
反對(duì) 回復(fù) 2023-09-16
?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

未觀察到的任務(wù)異常

await?SendUsersBirthdayEmailsAsync();?//?uses?TaskScheduler

由于您正在使用 TaskScheduler,請(qǐng)查看TaskScheduler.UnobservedTaskException。

當(dāng)故障任務(wù)的未觀察到的異常即將觸發(fā)異常升級(jí)策略時(shí)發(fā)生,默認(rèn)情況下,該策略將終止進(jìn)程


查看完整回答
反對(duì) 回復(fù) 2023-09-16
?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

正如這些方法的名稱所示,這些方法是相互獨(dú)立的,只需確保每個(gè)方法返回一個(gè)可等待的任務(wù)并將它們放入一個(gè)列表中即可。您使用的await建議,他們已經(jīng)這樣做了。


在它周圍放置一個(gè) try-catch 并捕獲 AggregateExeption。


List<Task> tasks = new List<Task>();

try

{

  tasks.Add(SendUsersBirthdayEmailsAsync());

  tasks.Add(DeleteOutdatedLogsAsync());

  tasks.Add(SendSystemNotificationsAsync());


  Task.WhenAll(tasks); // waits for all tasks to finish

}

catch (Exception e)

{

   //Log e.ToString(); will give you all inner exceptions of the aggregate exception as well, incl. StackTraces, so expect possibly a lot of chars, wherever you log it.

}

這也可能會(huì)加快速度,因?yàn)樗鼈儸F(xiàn)在是并行運(yùn)行的。但由于它們似乎都在數(shù)據(jù)庫上工作(很可能相同),因此由于調(diào)度開銷,即使有的話,也可能不會(huì)太多。


查看完整回答
反對(duì) 回復(fù) 2023-09-16
?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

我建議找出您從什么異常中async實(shí)際獲得此異常并了解您可以在 main 上使用它Thread

MyTask().GetAwaiter().GetResult();

這將允許您查看線程上實(shí)際異常的文本await。


如果您不希望主線程返回到await您也可以使用的行:

await SendUsersBirthdayEmailsAsync().ConfigureAwait(false);

這只會(huì)在最近的空閑線程上繼續(xù)您的任務(wù)。

但需要說的是,這在Android上會(huì)導(dǎo)致異常,因?yàn)樗鼘?duì)使用線程有一些限制。


如果您不想浪費(fèi)時(shí)間或不需要手動(dòng)處理異常并修復(fù)它trycatch finally始終是一個(gè)選擇。


查看完整回答
反對(duì) 回復(fù) 2023-09-16
?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

Task<Task> task = (SendUsersBirthdayEmailsAsync()

            .ContinueWith(x => Task.WhenAll(DeleteOutdatedLogsAsync(), SendSystemNotificationsAsync())));

await await task;

對(duì)于更一般的示例,提供了以下代碼:


class TaskTest

{

    public async void Start()

    {


        await (

            (await One().ContinueWith(x => Task.WhenAll(Two(), Three())))

            .ContinueWith(x=> Four()));

    }


    private async Task One()

    {

        await Task.Delay(5000);

        Console.WriteLine("1");

        throw new Exception();

    }


    private async Task Two()

    {

        await Task.Delay(2000);

        Console.WriteLine("2");

        throw new Exception();


    }

    private async Task Three()

    {

        await Task.Delay(3000);

        Console.WriteLine("3");

        throw new Exception();

    }


    private async Task Four()

    {

        await Task.Delay(1000);

        Console.WriteLine("4");

        throw new Exception();

    }

}

運(yùn)行此代碼表明,在任務(wù)內(nèi)引發(fā)異常不會(huì)停止整個(gè)程序。


查看完整回答
反對(duì) 回復(fù) 2023-09-16
  • 6 回答
  • 0 關(guān)注
  • 184 瀏覽

添加回答

舉報(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)