Main Content

addCause

例外の追加の原因を記録する

説明

baseException = addCause(baseException,causeException) は、causeException をその cause プロパティに追加することで、既存の MException オブジェクト baseException を変更します。結果の例外を try/catch ステートメントでキャッチすると、ベースとなる例外と共に追加のあらゆる原因レコードが作成され、エラー診断に活用することができます。

すべて折りたたむ

配列を作成し、logical 配列のインデックスを作成します。

A = [13 42; 7 20];
idx = [1 0 1; 0 1 0];

エラーに関する一般的な情報を提供する例外を作成します。インデックス配列をテストして、失敗の原因に関するより詳細な情報を例外に追加します。

try
    A(idx);
catch
    errID = 'MYFUN:BadIndex';
    msg = 'Unable to index into array.';
    baseException = MException(errID,msg);
    
    try
        assert(islogical(idx),'MYFUN:notLogical',...
            'Indexing array is not logical.')
    catch causeException
        baseException = addCause(baseException,causeException);
    end
    
    if any(size(idx) > size(A))
        errID = 'MYFUN:incorrectSize';
        msg = 'Indexing array is too large.';
        causeException2 = MException(errID,msg);
        baseException = addCause(baseException,causeException2);
    end
    throw(baseException)
end
Unable to index into array.

Caused by:
    Indexing array is not logical.
    Indexing array is too large.

baseException オブジェクトを調べます。

baseException
baseException = 

  MException with properties:

    identifier: 'MYFUN:BadIndex'
       message: 'Unable to index into array.'
         cause: {2x1 cell}
         stack: [0x1 struct]

cause プロパティの値は 2x1 の cell 配列です。

例外の 1 番目の原因を調べます。

baseException.cause{1}
ans = 

  MException with properties:

    identifier: 'MYFUN:notLogical'
       message: 'Indexing array is not logical.'
         cause: {0x1 cell}
         stack: [0x1 struct]

例外の 2 番目の原因を調べます。

baseException.cause{2}
ans = 

  MException with properties:

    identifier: 'MYFUN:incorrectSize'
       message: 'Indexing array is too large.'
         cause: {}
         stack: [0x1 struct]

入力引数

すべて折りたたむ

主な原因とエラーの場所を含む主な例外。MException オブジェクトとして指定します。

baseException に関連するエラーの原因と場所を含む関連する例外。MException オブジェクトとして指定します。

拡張機能

スレッドベースの環境
MATLAB® の backgroundPool を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool を使用してコードを高速化します。

バージョン履歴

R2007b で導入