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

11 vues (au cours des 30 derniers jours)
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!

Réponses (1)

Jaimin
Jaimin le 17 Sep 2024
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by