matlab can't catch error in subfunction

*爱你&永不变心* 提交于 2019-12-20 02:38:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!