lasterror - Last error message and related information

Syntax

s = lasterror
s = lasterror(err)
s = lasterror('reset')

Description

s = lasterror returns a structure s containing information about the most recent error issued by the MATLAB® software. The return structure contains the following fields:

FieldnameDescription

message

Character array containing the text of the error message.

identifier

Character array containing the message identifier of the error message. If the last error issued by MATLAB had no message identifier, then the identifier field is an empty character array.

stack

Structure providing information on the location of the error. The structure has fields file, name, and line, and is the same as the structure returned by the dbstack function. If lasterror returns no stack information, stack is a 0-by-1 structure having the same three fields.

The fields of the structure returned in stack are

FieldnameDescription

file

Name of the file in which the function generating the error appears. This field is the empty string if there is no file.

name

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

line

M-file line number where the error occurred.

See Message Identifiers in the MATLAB Programming Fundamentals documentation for more information on the syntax and usage of message identifiers.

s = lasterror(err) sets the last error information to the error message and identifier specified in the structure err. Subsequent invocations of lasterror return this new error information. The optional return structure s contains information on 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 return structure are empty strings, and the stack field is a 0-by-1 structure.

Remarks

The MathWorks is gradually transitioning MATLAB error handling to an object-oriented scheme that is based on the MException class. Although support for lasterror is expected to continue, using the static last method of MException is preferable.

Examples

Example 1

Save the following MATLAB code in an M-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
Input must be a vector.

Error in ==> average at 5
check_inputs(x)

Get the three fields from lasterror:

err = lasterror
err = 
       message: [1x61 char]
    identifier: 'AVG:NotAVector'
         stack: [2x1 struct]

Display the text of the error message:

msg = err.message
msg =
    Error using ==> average>check_inputs
    Input must be a vector.

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

Example 2

lasterror is often used in conjunction with the rethrow function in try-catch statements. For example,

try
   do_something
catch
   do_cleanup
   rethrow(lasterror)
end

See Also

last(MException), MException, try, catch, error, assert, rethrow, lastwarn, dbstack

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS