Desktop Tools and Development Environment Previous page   Next Page

Error Breakpoints

Set error breakpoints to stop program execution and enter debug mode when MATLAB encounters a problem. Unlike standard and conditional breakpoints, you do not set these breakpoints at a specific line in a specific file. Rather, once set, MATLAB stops at any line in any file when the error condition specified via the error breakpoint occurs. MATLAB then enters debug mode and opens the file containing the error, with the pause indicator at the line containing the error. Files open only when the you select Debug -> Open M-Files When Debugging. Error breakpoints remain in effect until you clear them or until you end the MATLAB session. You can set error breakpoints from the Debug menu in any desktop tool. This section covers the following topics:

Setting Error Breakpoints

To set error breakpoints, select Debug -> Stop if Errors/Warnings. In the resulting Stop if Errors/Warnings for All Files dialog box, specify error breakpoints on all appropriate tabs and click OK. To clear error breakpoints, select the Never stop if ... option for all appropriate tabs and click OK.

Image of Error/Warning Breakpoint dialog box.

For example, to pause execution when a warning occurs, select the Warnings tab, and from it select Always stop if warning, then click OK. When you run an M-file and MATLAB produces a warning, execution pauses, MATLAB enters debug mode, and the file opens in the Editor/Debugger at the line that produced the warning. To remove the warning breakpoint, select Never stop if warning in the Warnings tab and click OK.

Error Breakpoint Types and Options

The four basic types of error breakpoints you can set are Errors, Try/Catch Errors, Warnings, and NaN or Inf. Select the Always stop if ... option for each tab to set that type of breakpoint. Select the Use message identifiers ... option to limit each type of error breakpoint (except Nan or Inf) so that execution stops only for specific errors.

Errors.   When an error occurs, execution stops, unless the error is in a try...catch block. MATLAB enters debug mode and opens the M-file to the line that produced the error. You cannot resume execution.

Try/Catch Errors.   When an error occurs in a try...catch block, execution pauses. MATLAB enters debug mode and opens the M-file to the line that produced the error. You can resume execution or use debugging features.

Warnings.   When a warning is produced, MATLAB pauses, enters debug mode, and opens the M-file, paused at the line that produced the warning. You can resume execution or use debugging features.

NaN or Inf.   When MATLAB encounters a NaN (not-a-number) or Inf (infinite) value, it pauses, enters debug mode, and opens the M-file, paused at the line that encountered the value. You can resume execution or use debugging features.

Use Message Identifiers.   Execution stops only when MATLAB encounters one of the specified errors. This option is not available for the Nan or Inf type of error breakpoint. To use this feature:

  1. Select the Errors, Try/Catch Errors, or Warnings tab.
  2. Select the Use Message Identifiers option.
  3. Click the Add button.
  4. In the resulting Add Message Identifier dialog box, supply the message identifier of the specific error you want to stop for, where the identifier is of the form component:message, and click OK.
  5. The message identifier you added appears in the Stop If Errors/Warnings for All Files dialog box, where you click OK.
  1. You can add multiple message identifiers, and edit or remove them.

One way to obtain an error message identifier generated by a MATLAB function for example, is to produce the error, and then run the lasterror function. MATLAB returns the error message and identifier. Copy the identifier from the Command Window output and paste it into the Add Message Identifier dialog box. An example of an error message identifier is MATLAB:UndefinedFunction. Similarly, to obtain a warning message identifier, produce the warning and then run [m,id] = lastwarn; MATLAB returns the last warning identifier to id. An example of a warning message identifier is MATLAB:divideByZero.

Function Alternative for Error Breakpoints

The function equivalent for each option appears in the Stop if Errors/Warnings for All Files dialog box. For example, the function equivalent for Always stop if error is dbstop if error. Use the dbstop function with appropriate arguments to set error breakpoints from the Command Window, and use dbclear to clear them. Use dbstatus to view the error breakpoints currently set. Error breakpoints are listed in the cond field and message identifiers for breakpoints are listed in the identifier field of the dbstatus output.


Previous page  Breakpoints in Anonymous Functions Rapid Code Iteration Using Cells Next page

© 1994-2005 The MathWorks, Inc.