Throw error and display message


  • error(msg) example
  • error(msg,A1,...,An)
  • error(msgID,___)



error(msg) throws an error and displays an error message.

error(msg,A1,...,An) displays an error message that contains formatting conversion characters, such as those used with the MATLAB® sprintf function. Each conversion character in msg is converted to one of the values A1,...,An.

error(msgID,___) includes an error identifier on the exception. The identifier enables you to distinguish errors and to control what happens when MATLAB encounters the errors. You can include any of the input arguments in the previous syntaxes.


error(errorStruct) throws an error using the fields in a scalar structure.


collapse all

Throw Error

msg = 'Error occurred.';
Error occurred.

Throw Error with Formatted Message

Throw a formatted error message with a line break. You must specify more than one input argument with error if you want MATLAB to convert special characters (such as \n) in the error message string. Include information about the class of variable n in the error message.

n = 7;
if ~ischar(n)
   error('Error. \nInput must be a char, not a %s.',class(n))
Input must be a char, not a double.

If you only use one input argument with error, then MATLAB does not convert \n to a line break.

if ~ischar(n)
   error('Error. \nInput must be a char.')
Error. \nInput must be a char.

Throw an error with an identifier.

if ~ischar(n)
       'Error. \nInput must be a char, not a %s.',class(n))
Input must be a char, not a double.

Use the MException.last to view the last uncaught exception.

exception = MException.last
exception = 

  MException with properties:

    identifier: 'MyComponent:incorrectType'
       message: 'Error. 
Input must be a char, not a double.'
         cause: {0x1 cell}
         stack: [0x1 struct]

Throw Error Using Structure

Create structure with message and identifier fields. To keep the example simple, do not use the stack field.

errorStruct.message = 'Data file not found.';
errorStruct.identifier = 'MyFunction:fileNotFound';
errorStruct = 

       message: 'Data file not found.'
    identifier: 'MyFunction:fileNotFound'

Throw the error.

Data file not found.

Related Examples

Input Arguments

collapse all

msg — Information about errorstring

Information about the error, specified as a string. This message displays as the error message. To format the string, use escape sequences, such as \t or \n. You also can use any format specifiers supported by the sprintf function, such as %s or %d. Specify values for the conversion specifiers via the A1,...,An input arguments. For more information, see Formatting Strings.

    Note:   You must specify more than one input argument with error if you want MATLAB to convert special characters (such as \t, \n, %s, and %d) in the error message string.

Example: 'File not found.'

msgID — Identifier for errorstring

Identifier for the error, specified as a string. Use the error identifier to help identify the source of the error or to control a selected subset of the errors in your program.

The error identifier includes a component and mneumonic. The identifier must always contain a colon and follows this simple format: component:mnemonic. The component and mneumonic fields must each begin with a letter. The remaining characters can be alphanumerics (A–Z, a–z, 0–9) and underscores. No whitespace characters can appear anywhere in msgID. For more information, see Message Identifiers.

Example: 'MATLAB:singularMatrix'

Example: 'MATLAB:narginchk:notEnoughInputs'

A1,...,An — Numeric or character arraysscalar | vector | matrix | multidimensional array

Numeric or character arrays, specified as a scalar, vector, matrix, or multidimensional array. This input argument provides the values that correspond to and replace the conversion specifiers in msg.

errorStruct — Error reporting informationscalar structure

Error reporting information, specified as a scalar structure. The structure must contain at least one of these fields.


Error message string. For more information, see msg.


Error message identifier. For more information, see msgID.


Stack field for the error. When errorStruct includes a stack field, error uses it to set the stack field of the error. When you specify stack, use the absolute file name and the entire sequence of functions that nests the function in the stack frame. This string is the same as the string returned by dbstack('-completenames').

More About

collapse all


  • When you throw an error, MATLAB captures information about it and stores it in a data structure that is an object of the MException class. You can access information in the exception object by using try/catch. Or, if your program terminates because of an exception and returns control to the Command Prompt, you can use MException.last.

  • MATLAB does not cease execution of a program if an error occurs within a try block. In this case, MATLAB passes control to the catch block.

  • If all inputs to error are empty, MATLAB does not throw an error.

Introduced before R2006a

Was this topic helpful?