How to access the error line number where an error occurred with try/catch MException? App Designer

조회 수: 10 (최근 30일)
Hi,
I've developed an App Designer App and incorporated try-catch statements to enhance stability. Now, I aim to display information about where an error occurred. To achieve this, I have the following structure:
ErrorInfo = 'DeleteDataButtonPushed';
try
%Code to be executed
catch ME
DisplayErrorMsg(app, ErrorInfo, ME);
end
function DisplayErrorMsg(app, ErrorInfo, ME)
MsgError = ME.message;
MsgId = ME.identifier;
MsgLine = ME.stack.line; % ME.stack.line contains sometimes more than one line
Msg = ['Error in Line ' MsgLine newline MsgError newline MsgId newline ErrorInfo newline app.ErrorMsg];
uiwait(msgbox(Msg,"Error","error"));
end
My Problem is: ME.stack.line contains sometimes more than one line:
ME.stack.line
ans =
263
ans =
4275
ans =
62
How to access the imformation in the second row/ans?
Things that I tried:
ME.stack
ans =
3×1 struct array with fields:
file
name
line
size(ME.stack.line)
ans =
1 1
ME.stack.line(2) -- Intermediate dot '.' indexing produced a comma-separated list with 3 values, but it must produce a single value when followed by subsequent indexing operations.
ME.stack.line{2}-- Intermediate dot '.' indexing produced a comma-separated list with 3 values, but it must produce a single value when followed by subsequent indexing operations.
Msg = getReport(ME); finds all the lines, but the text it generates is huge and not very practical.
Thanks for your support!

답변 (1개)

Jaimin
Jaimin 2024년 9월 17일
To display all error messages using the msgbox function, iterate through all error lines from ME.stack array.
I have attached a sample code for better understanding.
function DisplayErrorMsg(app,ErrorInfo, ME)
MsgError = ME.message;
MsgId = ME.identifier;
% Initialize an empty string to store line information
MsgLines = '';
% Iterate over each element in the ME.stack array
for i = 1:length(ME.stack)
% Append each line number to the MsgLines string
MsgLines = [MsgLines, num2str(ME.stack(i).line), ' '];
end
% Construct the full error message
Msg = ['Error in Lines: ' MsgLines newline ...
'Error Message: ' MsgError newline ...
'Error ID: ' MsgId newline ...
'Error Info: ' ErrorInfo];
% Display the error message in a message box
uiwait(msgbox(Msg, "Error", "error"));
end
I hope this will be helpful.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by