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

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

消除由于不可預(yù)見(jiàn)的錯(cuò)誤而導(dǎo)致的一組功能的不完整執(zhí)行(JavaScript)

消除由于不可預(yù)見(jiàn)的錯(cuò)誤而導(dǎo)致的一組功能的不完整執(zhí)行(JavaScript)

catspeake 2023-06-09 10:43:47
我仍在學(xué)習(xí)這門(mén)語(yǔ)言,而且我非常想知道當(dāng)一個(gè)動(dòng)作需要執(zhí)行一系列函數(shù)時(shí),確保所有函數(shù)都將執(zhí)行或不執(zhí)行的正確方法是什么。例如,我可能有一個(gè)調(diào)用某些 apply() 函數(shù)的 HTML 按鈕:function apply() {  try {  // Check arguments, choose what exactly to do next through some IFs etc...  }  anotherFunction();}function anotherFunction() {  try {    // Request data from DB, process data received, update object variables, etc...  }  yetAnotherFunction();}function yetAnotherFunction() {  try {    // Update HTML  }  oneMoreFunction();}function oneMoreFunction() {  try {    // Update graph  }}所以這里的問(wèn)題是,如果流程中的任何函數(shù)拋出錯(cuò)誤,其余函數(shù)將不會(huì)執(zhí)行它們應(yīng)該執(zhí)行的操作,因此整個(gè) Apply 過(guò)程將被應(yīng)用一些更改而中斷(假設(shè) HTML 正在更新)但是休息(圖表)不是。我很想知道防止這種行為的最佳做法是什么?是的,我正在盡最大努力使用 try {} 并檢查參數(shù)是否有錯(cuò)誤等,但看起來(lái)我無(wú)法預(yù)見(jiàn)一切,我只需要一些方法來(lái)告訴代碼“確保您可以執(zhí)行所有功能,在如果出現(xiàn)任何錯(cuò)誤,根本不要做任何事情”。請(qǐng)告知這里可以做什么?
查看完整描述

1 回答

?
aluckdog

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

在考慮 try/catch 塊時(shí),您走的是正確的道路,但請(qǐng)注意我也使用了“catch”。通常(也許這甚至是強(qiáng)制執(zhí)行的,我不記得了)你需要 catch 塊和 try。


所以你的函數(shù)看起來(lái)像這樣:


function async myFirstTryCatch() {

   try {

     // Make your request in the try block

     await requestCall();

   } catch(error){

      // Hey, my http call returned an error

      // Deal with the error here. Maybe show a toast, validate a form

      // Anything you need to not break the code and have good UX

      console.log(error)

   }

}

按照同樣的思路,您可以讓每個(gè)函數(shù)處理自己的 try/catch,或者在您的應(yīng)用函數(shù)中控制它,以防某些鏈必須繼續(xù)/停止相互依賴(lài)。


    function apply() {

        try {

           firstCall();

           functionThatRequiresFirstCalltoSucceed();

        } catch (error){

          //Will catch if either firstCall or  functionThatRequiresFirstCalltoSucceed fail

          console.log(error)

        }


        functionThatIndependsFromTheResultAbove();

    }

我希望這會(huì)幫助你建立關(guān)于 JS 錯(cuò)誤處理的想法 :)


重要說(shuō)明 如果您的代碼進(jìn)入 catch 塊,它將認(rèn)為錯(cuò)誤已被處理并且不會(huì)傳播!這是一個(gè)例子


function functionThatThrowsError(){

  try{

    throw new Error('Example Error!');

  } catch (error) {

     // Error has been dealt with

     console.log(error) // Returns "Example Error"


     // throw error;   <--- Throw the error in the catch block if you need t to propagate

  }

}


 function wontCatchError() {

   try {

      functionThatThrowsError();

   } catch (error) {

      // THE CODE WILL NOT ENTER THE CATCH BLOCK

      // SINCE THE ERROR WAS CAUGHT IN THE FUNCTION ITSELF.


      // If you need to catch here as well, make sure to throw the error

      // in the catch block of the 'functionThatThrowsError'

      console.log(error)

   }

 }


查看完整回答
反對(duì) 回復(fù) 2023-06-09
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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