MATLAB - try...catch 語句



MATLAB 中的 try...catch 語句用於處理程式碼執行期間可能發生的錯誤和異常。它允許你優雅地處理錯誤,防止程式崩潰,並向用戶提供詳細的反饋。

讓我們檢查一下 try..catch 語句的語法。

語法

try
   statements
catch exception
   statements
end

語法的詳細解釋如下:

try

try 塊包含你認為可能產生錯誤的程式碼。這是你要監控錯誤異常的程式碼部分。當 MATLAB 在 try 塊中遇到錯誤時,它會立即跳轉到相應的 catch 塊。

catch 異常

catch 塊用於處理在 try 塊中捕獲的錯誤。它以關鍵字 catch 開頭,後跟變數 exception,它可以是你選擇的任何有效的變數名。此變數會自動填充發生錯誤的資訊。你可以使用 exception 來訪問與錯誤相關的資訊,例如錯誤訊息、識別符號等等。

exception 變數是可選的;如果你不需要訪問有關錯誤的資訊,可以選擇省略它。但是,它通常用於分析或記錄錯誤詳細資訊。

try 和 catch 塊中的語句

在 try 和 catch 塊中,你可以包含一個或多個 MATLAB 語句。在 try 塊中,這些是可能發生錯誤的語句。在 catch 塊中,這些是你希望在捕獲錯誤時執行的語句。

MATLAB 中 try...catch 語句的示例

讓我們看幾個示例,這些示例展示了 try..catch 在 matlab 中的工作方式。

示例 1:處理特定錯誤

讓我們在 try...catch 塊中執行以下程式碼,該程式碼故意嘗試訪問未定義的變數:

try
   result = undefined_variable
   fprintf('The result is: %f\n')
catch exception
   fprintf('An error occurred: %s\n', exception.message)
end

當你在 matlab 中執行以上程式碼時,輸出為:

>> try
   result = undefined_variable
   fprintf('The result is: %f\n')
catch exception
   fprintf('An error occurred: %s\n', exception.message)
end

An error occurred: Unrecognized function or variable 'undefined_variable'.

在上面的程式碼中,不允許訪問 undefined_variable,這將生成一個異常,導致 catch 塊執行並顯示一條錯誤訊息,指示該變數未定義。

示例 2:使用 try-catch 塊處理多個錯誤

在此示例中,程式碼嘗試訪問陣列索引超出範圍的索引。catch 塊處理此特定錯誤,對於其他意外錯誤,將顯示不同的訊息。

try
   y = [1, 2, 3];
   z = y(4) % Accessing an out-of-bounds index will generate an error
catch exception
   if strcmp(exception.identifier, 'MATLAB:badsubscript')
      fprintf('Index out of bounds error: %s\n', exception.message);
   else
      fprintf('An unexpected error occurred: %s\n', exception.message);
   end
end

當你在 Matlab 命令視窗中執行時,輸出為:

>> try
   y = [1, 2, 3];
   z = y(4) % Accessing an out-of-bounds index will generate an error
catch exception
   if strcmp(exception.identifier, 'MATLAB:badsubscript')
      fprintf('Index out of bounds error: %s\n', exception.message);
   else
      fprintf('An unexpected error occurred: %s\n', exception.message);
   end
end

Index out of bounds error: Index exceeds the number of array elements. Index must not exceed 3.

示例 3:另一個顯示不同型別錯誤的示例

此示例顯示如何使用 try...catch 結構處理不同型別的異常,其中檢查每種異常型別,並根據特定異常型別提供自定義警告訊息和操作,確保你的程式碼對錯誤做出優雅的響應。

try
   result = customFunction(5, 6);
catch exception
   switch exception.identifier
      case 'MATLAB:UndefinedFunction'
         warning('The custom function is not defined. Assigning a value of -1.');
         result = -1;
      case 'MATLAB:scriptNotAFunction'
         warning(['Attempting to execute a script as a function. '...
               'Running the script and assigning the output a value of 0.']);
         customFunctionScript;
         result = 0;
      otherwise
         rethrow(exception)
   end
end

讓我們在 matlab 命令視窗中測試如下所示:

>> try
   result = customFunction(5, 6);
catch exception
   switch exception.identifier
      case 'MATLAB:UndefinedFunction'
         warning('The custom function is not defined. Assigning a value of -1.');
         result = -1;
      case 'MATLAB:scriptNotAFunction'
         warning(['Attempting to execute a script as a function. '...
               'Running the script and assigning the output a value of 0.']);
         customFunctionScript;
         result = 0;
      otherwise
         rethrow(exception)
   end
end

Warning: The custom function is not defined. Assigning a value of -1. 
>> 

關於 MATLAB 中 try-catch 塊的關鍵點

關於 MATLAB 中 try-catch 塊的關鍵點的詳細解釋如下:

單個 try 塊中的多個 catch 塊

與 Java 等程式語言不同,MATLAB 不允許在單個 try 塊中包含多個 catch 塊。換句話說,你不能直接在 try 塊中為處理各種型別的異常設定不同的 catch 部分。相反,你通常使用單個 catch 塊來捕獲和處理異常,然後你可以在該 catch 塊中使用條件語句或其他邏輯來處理不同型別的異常。

巢狀 try/catch 塊

為了處理異常或管理不同的錯誤處理方法,你可以巢狀完整的 try/catch 塊。這意味著你可以在另一個 try/catch 塊中包含一個 try/catch 塊,形成一個分層錯誤處理機制。這允許你在程式碼的不同級別捕獲和處理錯誤,從而在錯誤管理方面提供靈活性。

缺少 finally 塊

與其他程式語言(例如 Java)一樣,MATLAB 不支援在 try/catch 語句中使用 finally 塊。finally 塊通常用於需要執行的程式碼,無論是否發生異常。在 MATLAB 中,你需要將此類程式碼放在 try/catch 塊之外,以確保它始終執行,無論是否丟擲異常。

廣告
© . All rights reserved.