Print full stack trace of both Cause and Base Exceptions

22 ビュー (過去 30 日間)
Veeresh Rudresh
Veeresh Rudresh 2023 年 1 月 30 日
編集済み: Rik 2023 年 3 月 21 日
How to print the full stack trace (with line numbers) of both cause and base exceptions? The basic structure of my main script is below:
% Main Script
try
some_other_script;
catch ME1
if strcmp(ME1.identifier, 'Test:SetUpFailure')
% save setup failure clean up actions here
end
rethrow(ME1);
end
Another script i.e., 'some_other_script' has the structure as below:
try
do_some_setup_here;
catch causeException
baseException = MException('Test:SetUpFailure', 'Something broke in setup.');
baseException = addCause(baseException, causeException);
throw(baseException);
end
do_other_things_here;
If an exception occurs, then it prints the following.
Error using some_other_script (line 6)
Something broke in setup.
Error in main_script (line 2)
some_other_script;
Caused by:
Subscript indices must either be real positive integers or logicals.
But it does not print the stack trace of the exception "Subscript indices must either be real positive integers or logicals.". I know I can print the stack trace using getReport(). But I think it will be nice and easy to see the full stack trace of both base and cause exceptions in one place. So, is there a way to do this?
  1 件のコメント
Rik
Rik 2023 年 1 月 30 日
編集済み: Rik 2023 年 3 月 21 日
My first instinct is to either write the required data to some shared variable, or to generate the full error message in the inner function. I haven't published my exception redirection functions separately yet, but you can find a version of them here. Perhaps they will give you some inspiration for how to solve the problem on your own.
I suspect you could tweak the input to addCause to get what you want.

サインインしてコメントする。

回答 (1 件)

Yash Srivastava
Yash Srivastava 2023 年 3 月 21 日
Hi Veeresh
In my understanding you want to print full stack trace for both, base and cause exception. Currently there is no direct way to print stack trace of both exceptions, but you can tweak your code little bit and display the stack using 'disp' method.
Please refer to the following example based on the code structure provided by you.
clc
clear
A = [13 42; 7 20];
idx = [1 0 1; 0 1 0];
try
try
A(idx);
catch causeException
baseException = MException('WrongIndex:illegalIndexing', 'Something broke in setup.');
baseException = addCause(baseException, causeException);
throw(baseException);
end
catch ME1
disp('Cause Exception line number-')
disp(ME1.cause{1}.stack.line)
rethrow(ME1);
end
Here, the exception is thrown by line 7
A(idx);
which is caught by 'causeException'. The stack trace of 'baseException' will be empty since it does not directly catch any exception, but the 'cause' property will be set to 'causeException'. Since, 'baseException' is being thrown again, it will be caught by 'ME1' whose cause property will be same as that of 'baseException'. You can display the stack of 'causeException' using
disp(ME1.cause{1}.stack)
which will print the complete stack of 'causeException'. To display only the line number you can refer to 'line' property of stack struct.
disp('Cause Exception line number-')
disp(ME1.cause{1}.stack.line)
The output of the above code for the given example will be
Cause Exception line number-
7

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by