首頁  >  文章  >  web前端  >  如何解決Typescript中過多的try catch?

如何解決Typescript中過多的try catch?

WBOY
WBOY轉載
2023-09-12 13:01:02884瀏覽

如何解决Typescript中过多的try catch?

我們可以使用 try-catch 語句來解決 TypeScript 中的錯誤。有時,我們需要在程式碼中新增多個try-catch區塊來處理多個錯誤。

當我們在程式碼中加入多個try-catch語句時,程式碼變得不可讀,重構也成為開發者頭痛的問題。在本教程中,我們將學習將過多的 try-catch 區塊轉換為可以管理多個錯誤的單一 try-catch 區塊。

文法

使用者可以依照下列語法在 TypeScript 中使用單一 try-catch 區塊。

try {
   throw new Error("error_message");
   // this code will not be executed
}
catch (error) {
   // manage the error
}

在上面的語法中,我們將錯誤拋出到 try 區塊中,並在 catch 區塊中捕獲錯誤。

每當我們在 try 區塊中遇到任何錯誤時,執行控制都會直接轉到 catch 語句,而不執行其他 try 區塊程式碼。

範例 1:try-catch 區塊過多

在下面的範例中,我們新增了四個 try-catch 區塊。我們從每個 try-catch 區塊中拋出帶有不同訊息的錯誤。

使用者可以看到輸出中每個 catch 區塊列印的錯誤訊息。

try {
   console.log("Inside the first try block");
   throw new Error("first error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the second try block");
   throw new Error("second error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the third try block");
   throw new Error("Third error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the fourth try block");
   throw new Error("Fourth error message");
} catch (error) {
   console.log(error.message);
}

編譯時,它將產生以下 JavaScript 程式碼。

try {
   console.log("Inside the first try block");
   throw new Error("first error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the second try block");
   throw new Error("second error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the third try block");
   throw new Error("Third error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the fourth try block");
   throw new Error("Fourth error message");
}
catch (error) {
   console.log(error.message);
}

輸出

上述程式碼將產生以下輸出 -

Inside the first try block
first error message
Inside the second try block
second error message
Inside the third try block
Third error message
Inside the fourth try block
Fourth error message

從上面的例子中,使用者可以了解到當我們在單一程式碼中使用過多的 try-catch 語句時,程式碼是如何變得不可讀且不清楚的。

現在,我們將學習使用單一 try-catch 區塊來處理具有不同錯誤的多個程式碼區塊。

範例 2

在下面的範例中,我們建立了solveProblems() 函數。它接受一個函數作為參數並在 try 區塊中呼叫函數。如果函數拋出任何錯誤,我們可以在單一區塊中捕獲它。

function func2() {
   throw new Error("This error is from second function!");
}

function func3() {
   let num = 10;
   num.toPrecision(1000);
}
function solveProblems(func: any) {
   // calling the callback function in the try block
   try {
      func();
   } catch (error) {
      console.log(error.message);
   }
}

// calling functions
solveProblems(func2);
solveProblems(func3);

編譯時,它將產生以下 JavaScript 程式碼 -

function func2() {
   throw new Error("This error is from second function!");
}
function func3() {
   var num = 10;
   num.toPrecision(1000);
}
function solveProblems(func) {
   // calling the callback function in the try block
   try {
      func();
   }
   catch (error) {
      console.log(error.message);
   }
}
// calling functions
solveProblems(func2);
solveProblems(func3);

輸出

上述程式碼將產生以下輸出 -

This error is from second function!
toPrecision() argument must be between 1 and 100

從上面的範例中,使用者可以了解如何透過將多個 try-catch 區塊替換為單一 try-catch 區塊來刪除多個 try-catch 區塊。使用者只需為每個單獨的程式碼區塊建立一個單獨的函數,並可以在單一 try 區塊中逐一呼叫每個函數。

以上是如何解決Typescript中過多的try catch?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除