How to access the error line number where an error occurred with try/catch MException? App Designer
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (1)
Jaimin
am 17 Sep. 2024
Hello @Marcel Rapp
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!