Reputation: 1346
I am building a GUI in MATLAB (2016a) which I will be compiling and deploying. I want to try to do some global error handling, and it occurs to me that any command given to the GUI (button click, etc) first goes through the main initialization code before going to the specific Callback function. My thought was to put a try-catch
block around the calls to gui_mainfcn
. What's making me hesitate is that the code is bookended by some big old warnings:
% Begin initialization code - DO NOT EDIT
... initialization code here ...
% End initialization code - DO NOT EDIT
Could I break something by putting a try-catch
block inside this initialization section? Is there a better way to attempt global error handling for a single GUI?
Upvotes: 1
Views: 92
Reputation: 65430
There is no reason that you can't insert global error handling in the main function of your GUIDE GUI. The warnings are really there to prevent people from inadvertently disrupting GUI functionality. In your case, a try
/catch
isn't going to actually modify the functionality so you're fine. You just want to be sure to not remove the calls to gui_mainfcn
which is an internal function which contains all of the GUI logic.
Aside from that, you will also want to ensure that all requested output arguments are populated so that in the case of an error (for a function call where an output argument is expected) no error (within your catch
block) is thrown because of that. That should be easy enough though
Also, I would only wrap the calls to gui_mainfcn
try
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
catch ME
% Go ahead and fill in the requested outputs with [] so we don't get an error
[varargout{1:nargout}] = deal([]);
% Do special error handling here
fprintf('Caught error: %s\n', ME.message);
end
Upvotes: 1