Main Content

lasterror

R2026b

(Not recommended) Last error message and related information

lasterror is not recommended. Use MException instead. For more information, see Version History.

Description

s = lasterror returns a structure s containing information about the most recent error issued by MATLAB®.

example

s = lasterror(errorStruct) sets the last error information to the error information specified in the structure errorStruct. Subsequent invocations of lasterror return this new error information. The optional return structure s contains information about the previous error.

s = lasterror("reset") sets the last error information to the default state. In this state, the message and identifier fields of the returned structure are empty character vectors, and the stack field is a 0-by-1 structure.

Examples

collapse all

Save the following MATLAB code in a file called average.m:

function y = average(x)
% AVERAGE Mean of vector elements.
% AVERAGE(X), where X is a vector, is the mean of vector elements.
% Nonvector input results in an error.
check_inputs(x)
y = sum(x)/length(x);      % The actual computation

function check_inputs(x)
[m,n] = size(x);
if (~((m == 1) || (n == 1)) || (m == 1 && n == 1))
    error('AVG:NotAVector', 'Input must be a vector.')
end

Now run the function. Because this function requires vector input, passing a scalar value to it forces an error. The error occurs in subroutine check_inputs:

average(200)

Error using average>check_inputs (line 11)
Input must be a vector.

Error in average (line 5)
check_inputs(x)

Get the three fields from lasterror:

err = lasterror
err = 
       message: 'Error using average>check_inputs (line 11)↵Input must be a vector.'
    identifier: 'AVG:NotAVector'
         stack: [2x1 struct]

Display the fields containing the stack information. err.stack is a 2-by-1 structure because it provides information on the failing subroutine check_inputs and also the outer, primary function average:

st1 = err.stack(1,1)
st1 = 
    file: 'd:\matlab_test\average.m'
    name: 'check_inputs'
    line: 11
st2 = err.stack(2,1)
st2 = 
    file: 'd:\matlab_test\average.m'
    name: 'average'
    line: 5

Note

As a rule, the name of your primary function should be the same as the name of the file that contains that function. If these names differ, MATLAB uses the file name in the name field of the stack structure.

Use lasterror in conjunction with the rethrow function in a try, catch statement.

try
   do_something
catch
   do_cleanup
   rethrow(lasterror)
end

Input Arguments

collapse all

Error reporting information, specified as a scalar structure. The structure must contain at least one of the fields in this table. The lasterror function ignores any additional fields in errorStruct.

FieldnameDescription
message

Error message, specified as a text scalar. The lasterror function displays the message as specified and does not convert special characters.

If you do not specify a value for this field, the error message defaults to ''.

identifier

Error identifier, specified as a text scalar. For more information about error identifiers, see MException.

If you do not specify a value for this field, the error identifier defaults to ''.

stack

Stack trace information for the error, specified as a structure array with the following fields. The structure array has the same format as the structure returned by dbstack("-completenames"). The lasterror function ignores any additional fields in stack.

  • file — Absolute path of the file in which the function appears.

  • name — Name of the function within the file.

  • line — Line number of the function call. If you specify a noninteger value, lasterror uses only the real, integer part. The lasterror function also replaces invalid line values, such as NaN or Inf, with 0.

If you do not specify a value for the stack field, the stack trace information for the error defaults to a 0-by-1 structure with the fields file, name, and line.

Output Arguments

collapse all

Last error information, returned as a scalar structure. The structure contains information from the most recent error issued by MATLAB, from the default state set by lasterror("reset"), or from the previous error when using lasterror(errorStruct). The structure contains the fields in this table.

FieldnameDescription

message

Error message, returned as a character array. lasterror("reset") sets this field to ''.

identifier

Error identifier, returned as a character array. If the last error issued by MATLAB had no error identifier, then the identifier field is ''. lasterror("reset") sets this field to ''.

stack

Stack trace information for the error, returned as a structure array with the following fields. The structure has the same format is the same as the structure returned by the dbstack function.

  • file — Absolute path of the file in which the function generating the error appears, returned as a character array. If there is no file, this field contains an empty character vector.

  • name — Name of the function in which the error occurred. If the error occurred in the primary function in the file, and the function name differs from the file name, name is set to the file name.

  • line — Line number of the file at which the error occurred.

If lasterror returns no stack information, stack is a 0-by-1 structure with the same three fields. lasterror("reset") sets this field to the same 0-by-1 structure.

Extended Capabilities

expand all

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced before R2006a

expand all