For future reference, this doesn't seem to cause any problems. It does catch any and all untrapped errors from the compiled code and you can put an error dialog in the catch portion or even a "close all" if you want to kill the GUI as a result of unknown errors. This does not get triggered if an error occurs inside any lower level try/catch blocks.
Is using a try/catch around gui_mainfcn a bad idea?
2 次查看(过去 30 天)
显示 更早的评论
I am working on a fairly large signal processing GUI created with GUIDE. The final product will be a compiled program and one the main issues I have been struggling with is that any unforseen errors are not brought to the users attention and so the software might be behaving improperly with no indication given to the user.
I have been trying to validate inputs and use a lot of try/catch statements where I can but given the size and number of functions this is pretty tedious.
Disregarding the warnings written by GUIDE, I added a try catch around the "gui_mainfcn(gui_State, varargin{:});" line within the top level function and it seems to have the exact behavior I was looking for: any untrapped, uncaught errors bring up a error message dialog rather than going to the command line.
Is this going to have any unintended consequences?
回答(2 个)
Staffan Neth
2018-8-21
Can I do the same in app designer? There is not such a "main call" in the code to wrap with try/catch? I cannot find this anywhere. Doing try/catch in each callback is tedious work in a large GUI.
cheers, Staffan
0 个评论
Daniel Shub
2013-10-29
Depending on how you implement your GUI this may not catch all errors. Callbacks run in a weird context. Consider the following class
classdef testclass < handle
properties( SetObservable )
x
h
end
methods
function obj = testclass
obj.h = addlistener(obj, 'x', 'PostSet', @(src,evt)callbackwitherror(obj, src, evt));
end
function callbackwitherror(~, ~, ~)
error('Callback:Error', 'My Error');
end
end
end
I would expect to see "Error caught" displayed with
h = testclass;
try
h.x = 10;
catch ME
disp('Error Caught');
end
but instead I see a bunch of information about the error I am trying to catch and the error is not caught. In earlier versions of MATLAB the "nonsense" was printed in black, now it is printed in red. A key to the problem is the first line of the output
Warning: Error occurred while executing callback:
You get a warning that an error occurred and not an error that an error occurred. This potentially makes sense depending on how you view the context in which a callback is run.
To work around this, I use a wrapper function mycallback that all my callbacks call. It takes 3 arguments: a function handle, the callback source, and the event data. The mycallback function wraps the execution of the function handle in a try-catch. Since we are still in the callback context within mycallback, errors are caught and I can set a flag and/or deal with the error. When dealing with the error (e.g., cleaning up), you need to make sure that you are not in nested callbacks otherwise you can really screw things up.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!