Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab can't catch error in subfunction

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?

like image 871
Chris Herrera Avatar asked Mar 24 '26 02:03

Chris Herrera


1 Answers

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.

like image 91
Daniel Avatar answered Mar 26 '26 16:03

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!