问题
I'm trying to implement a bug reporting system in my code so I put a try/catch around the function that is run to start the program. It's a programmatic GUI so most of the subfunctions are callbacks for buttons or other GUI elements. However whenever an error is thrown in these subfunctions, it is not caught. Some of the subfunctions are defined in other files as they are other programmatic GUI files.
My question is, is there anyway to catch errors that are more than one function level deep?
Example below: I run CeleST to start the program
function CeleST
try
% Global try-catch
CSTMainWindow()
catch exception
generateReport(exception) % bugReporter
end
CSTMainWindow is a programattic GUI file and here is one of the pushbuttons:
uicontrol('parent',mainPanel,'style','pushbutton','string','1. Process videos...','position',[500 yFilters+hFilters+10 170 60],'callback',@processVideo);
However errors in processVideo are not caught processVideo:
function processVideo(hObject,eventdata) %#ok<INUSD>
set(mainFigure,'Visible','off');
CSTProcessVideos % Programmatic GUI File for another window
set(mainFigure,'Visible','on');
flagConsistentButton = false;
checkSequences
populateFilters
end
Even putting undefined variables in the subfunctions throws errors but they're not caught by my try/catch. Any suggestions or am I doing something wrong? Do I really have to put try-catch blocks around everything?
回答1:
GTSMainWindow is not calling processVideo. Instead the function is used as a callback and called later. Basically every callback function must care for it's own errors, put the try catch into the processVideo function and it will catch the error.
来源:https://stackoverflow.com/questions/31970733/matlab-cant-catch-error-in-subfunction