Is using a try/catch around gui_mainfcn a bad idea?

3 Ansichten (letzte 30 Tage)
Jeremy
Jeremy am 15 Okt. 2013
Beantwortet: Staffan Neth am 21 Aug. 2018
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?
  1 Kommentar
Jeremy
Jeremy am 29 Okt. 2013
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Staffan Neth
Staffan Neth am 21 Aug. 2018
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

Daniel Shub
Daniel Shub am 29 Okt. 2013
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.

Kategorien

Mehr zu Environment and Settings 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!

Translated by