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]
入力引数
baseException
— 主な例外
MException
オブジェクト
主な原因とエラーの場所を含む主な例外。MException
オブジェクトとして指定します。
causeException
— 関連する例外
MException
オブジェクト
baseException
に関連するエラーの原因と場所を含む関連する例外。MException
オブジェクトとして指定します。
バージョン履歴
R2007b で導入
参考
throw
| rethrow
| throwAsCaller
| MException.last
| assert
| try, catch
| error
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)