TypeScript 中的錯誤處理
像 TypeScript 和 JavaScript 這樣的函數語言程式設計語言提供了一種使用try-catch塊處理錯誤的方法。try塊捕獲錯誤,catch塊處理錯誤。在本教程中,我們將學習如何在 TypeScript 中處理錯誤。
TypeScript 中主要有 7 種類型的錯誤。在這裡,我們將逐一學習所有型別的錯誤。
範圍錯誤− 如果我們嘗試訪問超出範圍的任何內容,TypeScript 將丟擲範圍錯誤。例如,訪問陣列索引以獲取像 10^100 這樣的大數值。
引用錯誤− 如果我們嘗試訪問未宣告的變數,TypeScript 將丟擲引用錯誤。例如,在未宣告字串變數的情況下,對該變數應用 toLowerCase() 方法。
語法錯誤− 如果我們使用錯誤的語法編寫程式碼,TypeScript 將丟擲語法錯誤。例如,10***10 是語法錯誤,因為 *** 在 TypeScript 中不代表任何運算子。
型別錯誤− 由於變數型別不匹配而發生。例如,使用 TypeScript 中的 Math.ceil() 方法刪除字串變數的小數部分。
URI 錯誤− 它可能在 TypeScript 中編碼或解碼 URI 時發生。例如,傳遞無效引數給 decodeURI() 函式會返回 URI 錯誤。
Eval 錯誤− 這些錯誤在 TypeScript 中使用 eval() 函式時發生。我們可以使用 eval() 函式來評估表示式。
內部錯誤− 這種錯誤在內部發生。例如,如果堆疊大小超出限制,可能會發生這種情況。
我們已經瞭解了不同型別的錯誤。現在,我們將學習如何使用 try、catch 和 finally 塊來處理錯誤。
使用 try、catch 和 finally 塊
第一個問題出現了——什麼是 try、catch 和 finally 塊?try 塊可以包含有錯誤的程式碼。如果發生任何錯誤,它將丟擲錯誤並停止從發生錯誤的行執行程式碼。
顧名思義,catch 塊用於捕獲和處理錯誤。我們可以根據不同的錯誤在 catch 塊中編寫處理錯誤的程式碼。一個 try 塊可以有多個 catch 塊。我們可以建立一個不同的 catch 塊來處理不同型別的錯誤,例如語法錯誤和引用錯誤。
finally 塊包含在程式終止之前始終需要執行的程式碼。例如,關閉資料庫連線或關閉檔案,我們在錯誤發生之前在 try 塊中開啟的檔案。
語法
使用者可以按照以下語法使用 try、catch 和 finally 塊。
try {
// Code with error
} catch {
// handle the error
} finally {
// execute the code before the program termination
}
示例 1
在下面的示例中,我們演示瞭如何使用 try-catch 塊來處理 TypeScript 中的錯誤。在 try 塊中,我們透過建立錯誤的新例項來丟擲錯誤。catch 塊捕獲並處理該錯誤,透過在螢幕上列印一些訊息和錯誤來處理。此外,使用者可以在輸出中看到“try 塊結束”訊息未列印,因為它是在錯誤發生行之後的一行中編寫的。
try {
// This should generate syntax error.
throw new SyntaxError("Error occured inside the syntax.");
// this code will not execute.
console.log("End of the try block");
} catch (error) {
// control will be at here, after throwing the error from try block.
console.log("Inside the catch block.");
console.log("The error is " + error);
}
編譯後,它將生成以下 JavaScript 程式碼:
try {
// This should generate syntax error.
throw new SyntaxError("Error occured inside the syntax.");
// this code will not execute.
console.log("End of the try block");
}
catch (error) {
// control will be at here, after throwing the error from try block.
console.log("Inside the catch block.");
console.log("The error is " + error);
}
輸出
以上程式碼將產生以下輸出:
Inside the catch block. The error is SyntaxError: Error occured inside the syntax.
示例 2
在下面的示例中,我們在 try 塊中建立了函式。它接受兩個引數。第一個引數可以是任何值;另一個應該是數字。此外,它始終返回數字或 ReferenceError 物件。
在 errofunc() 函式內部,我們檢查如果兩個引數都是數字,則返回數字的和;否則,返回錯誤。在輸出中,我們可以看到第一個函式呼叫將正確執行,第二個函式呼叫將建立錯誤。
try {
// Creating the function which returns an number or error
function errorfunc(num1, num2: number): number | ReferenceError {
// If both parameters of type number, then return addition.
// Otherwise throw reference error.
if (typeof num1 == "number" && typeof num2 == "number") {
console.log(num1 + num2);
}
// Throwing the new instance of the reference error.
throw new ReferenceError("Parameter/s are not type of numbers.");
}
// This will throw an error.
errorfunc("11", 12);
} catch (error) {
console.log("Inside the catch block.");
console.log("The error is " + error);
} finally {
// This will execute always
console.log(
"Inside the finally block to execute the code before program termination."
);
}
編譯後,它將生成以下 JavaScript 程式碼:
try {
// Creating the function which returns an number or error
function errorfunc(num1, num2) {
// If both parameters of type number, then return addition.
// Otherwise throw reference error.
if (typeof num1 == "number" && typeof num2 == "number") {
console.log(num1 + num2);
}
// Throwing the new instance of the reference error.
throw new ReferenceError("Parameter/s are not type of number.");
}
// This will throw an error.
errorfunc("11", 12);
}
catch (error) {
console.log("Inside the catch block.");
console.log("The error is " + error);
}
finally {
// This will execute always
console.log("Inside the finally block to execute the code before program termination.");
}
輸出
以上程式碼將產生以下輸出:
Inside the catch block. The error is ReferenceError: Parameter/s are not type of number. Inside the finally block to execute the code before program termination.
在本教程中,我們學習瞭如何處理錯誤。我們使用 try、catch 和 finally 塊來處理錯誤。但是,在 TypeScript 中處理錯誤類似於在 JavaScript 中處理錯誤,但我們也必須關注 TypeScript 中的錯誤型別。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP