Errors in listener callbacks
조회 수: 27 (최근 30일)
이전 댓글 표시
I have an app built in MATLAB App Designer. the underlying code utlizes events and listeners. my problem is this: Whenever an error occurs in the code, the app needs to know about this. when the error occurs in the code itself, there are built in "catch" commands that notify the GUI.
However, when an error occurs in one of the listener callbacks, there is some built in mechanism that replaces the error with a warning. Instead of getting an error message we get a warning: "error occurred during execution of listener callback" and a description of the error message.
Is there a way to cancel this mechanism? to let the errors in listener callbacks become actual error messages? or maybe a way to notify the app in case of a warning? something like try catch that includes warnings?
many thanks
Nathan
댓글 수: 0
채택된 답변
Adam Danz
2023년 5월 31일
편집: Adam Danz
2023년 5월 31일
> Is there a way to cancel this mechanism? to let the errors in listener callbacks become actual error messages?
No, there isn't an off-switch to this behavior.
> or maybe a way to notify the app in case of a warning?
It's unclear what listeners you are refering to but I'll assume they were added by the developer of the app rather than by app-designer. One idea is to wrap the listener's callback function within a try/catch and to take some action within the catch block when there is an error. However, warnings are not caught by the try-block.
Another idea is to use lastwarn to clear the last-warning-history at the beginning of the listener's callback and then to query lastwarn again at the end of the callback to determine if a warning was thrown.
Neither of these potential solutions would catch warnings that are thrown prior to or after reaching the callback function.
lastwarn('') % clear last warning
% < function code goes here >
[msg, id] = lastwarn();
if ~isempty(msg)
% take action on warning
end
댓글 수: 5
Adam Danz
2024년 2월 18일
I get where you're coming from, and I can see how this behavoir might feel like a head-scratcher. "Why" questions about long-established decisions can be tough to address. While I can't shed light on the historical 'why' behind this particular design choice, I could offer suggestions.
> we can decide if the "single faulty listener" should "disrupt the entire system" or not
I'm not sure that I follow this argument without a clarifying use case example. Listener callbacks have their own workspace so even if a listener callback would throw an error it wouldn't affect or disrupt any processes outside of that workspace. When there is an error within a listener callback, execution stops at that line which is a disruption to the processes within that workspace.
Try/catch statements still function within listener callbacks, although they can't be used to rethrow an error. They are still an effective way to detect and react to errors within the listener callback.
Here's an example.
I've created a faulty listener that updates the color of a marker any time the axes limit change. However, the marker no longer exists which causes an invalid or deleted object error in the listener callback.
I've caught the error and displayed the error message as red text along with a beep to simulate a MATLAB error.
Run this from a script and then manually drag the axes. Make sure your speakers are on.
figure
ax = axes;
h = plot(.5,.5,'+','MarkerSize',40,'LineWidth',5);
addlistener(ax, {'XLim', 'YLim'}, 'PostSet', @(~,~)listenerFcn(h));
delete(h)
function listenerFcn(h)
% Responds to changes to axis limits
try
h.Color = rand(1,3);
catch ME
% Simulate error
beep
fprintf(2,[ME.message,newline])
end
end
You'll see a burst of successive beeps along with a stack of error messages.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!