User Tools

Site Tools


en:software:matlab:trepr:dev:gui:robust

Robust Programming

“Graceful exiting” is one of the important aspects especially of GUI programming. There will always be bugs in your functions. Additionally, intrinsically, one cannot predict the user's next action.

Robust programming has at least two aspects:

  • Use try-catch constructs.
  • Make use of error reporting infrastructure of the toolbox.

Try-Catch Constructs

One of the programming principles that MathWorks, Inc. included only as late as Matlab™ 2009b was the proper exception handling. As this is very useful for larger projects, here we have one reason why the toolbox is not compatible to earlier Matlab™ versions.

The basic structure of a try-catch construct is as follows:

try
    % Do something useful
catch exception
    % If something went wrong, handle the exception
    throw(exception)
end

This very basic example of the try-catch structure would not be very useful in actual code. To give you a more complete (real-world) example of a catch-branch that actually makes use of the infrastructure for error handling provided by the toolbox:

function someRandomFunction()
    try
        % Do something useful
    catch exception
        try
            msgStr = ['An exception occurred in ' ...
                exception.stack(1).name  '.'];
            trEPRmsg(msgStr,'error');
        catch exception2
            exception = addCause(exception2, exception);
            disp(msgStr);
        end
        try
            trEPRgui_bugreportwindow(exception);
        catch exception3
            % If even displaying the bug report window fails...
            exception = addCause(exception3, exception);
            throw(exception);
        end
    end
end

Here, you see the example of a very defensive programming strategy. First, the function tries to add a status message to the GUI status, and if that fails, it adds another exception to the first one. Second, it tries to call the bug reporter from the toolbox, and if that fails as well, it will finally throw the exception, after adding an exception for not being able to open the bug reporter. In this final case, there is nothing left to do than throwing an exception.

Error Reporting Infrastructure

To help both programmers and users to properly report bugs and exceptions appearing while using the toolbox, there is a function helping the user to collect all the information necessary for a meaningful bug report: trEPRgui_bugreportwindow().

In the above example, you could already see how to make use of this bug reporting tool inside a catch branch of a try-catch structure. Additionally, you could see how the catch branch does not rely on any further bug reporting infrastructure. Nevertheless, it tries first of all to add a status message to the GUI, and afterwards to open the bug reporter. If everything fails, it will just throw the exception, as in this case there is nothing else left to do.

Fig. 1 gives you an idea of how the bug reporter window will look like that opens in case of an exception.

Figure 1: The bug reporter window that opens in case of an exception. The window collects all necessary information for filing a helpful bug report. Pressing the <key>Report</key> button will open the BugZilla URL in a browser window and let the user report the bug. Furthermore, the bug report can be saved by pressing <key>Save</key>.

The pink colour is chosen to make the user pay attention. The buttons at the bottom left of the window allow for reporting the bug (via opening the BugZilla URL in a browser window) and saving it to a text file.

en/software/matlab/trepr/dev/gui/robust.txt · Last modified: 2020/09/30 21:35 by 127.0.0.1