checkTryExceptionHandling

Run this check to check if all try blocks are only used for exception handling. Although not recommended, try statements could also be used to suppress errors or to express a simple condition. checkTryExceptionHandling checks the following for every try:

  1. Is there a catch block with at least one line of executable code?

  2. Is an exception object assigned or created in the catch?

    1. Assigned: catch`` ME

    2. Created: ME = MException(errorID, msg);

  3. Is at least one of the list of exception handling MATLAB-installed functions or keywords used in the catch?

This does not check if points 2 and 3 hold for all execution paths within the catch, so if you use an if-else within the catch and only call an exception handling function in one of the two, it is not reported by this check.

Configurable parameters

  • RequireExceptionObject (boolean): When set to true (default), perform check 2. When set to false, do not require an exception object to be assigned or created in the catch.

  • ExceptionHandlingFunctions (list): List of functions or MATLAB keywords (return etc.), at least one of which must be called within every catch. The default list is error, throw, rethrow, throwAsCaller. Other suggestions are: errordlg, warning, return or some user-defined exception handling function.

Exemption tag: %@ok<TRYEH>