Print full stack trace of both Cause and Base Exceptions

30 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by