lasterror
R2026b(Not recommended) Last error message and related information
lasterror is not recommended. Use MException instead. For more information, see Version History.
Description
returns a structure
s = lasterrors containing information about the most recent error issued by
MATLAB®.
sets the last error information to the error information specified in the structure
s = lasterror(errorStruct)errorStruct. Subsequent invocations of lasterror
return this new error information. The optional return structure s
contains information about the previous error.
sets the last error
information to the default state. In this state, the s = lasterror("reset")message and
identifier fields of the returned structure are empty character
vectors, and the stack field is a 0-by-1 structure.
Examples
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: 11st2 = err.stack(2,1)
st2 =
file: 'd:\matlab_test\average.m'
name: 'average'
line: 5Note
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
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.
| Fieldname | Description |
|---|---|
message | Error message, specified as a text scalar. The
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 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
If you do not specify a value for the
|
Output Arguments
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.
| Fieldname | Description |
|---|---|
| Error message, returned as a character array.
|
| Error identifier, returned as a character array. If the last error
issued by MATLAB had no error identifier, then the |
| 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
If |
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.
Version History
Introduced before R2006aAdditional fields in errorStruct.stack are now ignored. Previously,
lasterror preserved these additional fields when setting the last error
using lasterror(errorStruct).
In addition, if the line field in
errorStruct.stack contains a noninteger value,
lasterror uses only the real, integer part. If the
line field contains an invalid value, such as NaN or
Inf, lasterror replaces the value with
0. Previously, lasterror preserved invalid or
unsupported values when setting the last error using
lasterror(errorStruct).
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 MException.last method of
MException is preferable.
Warning
lasterror and MException.last are not
guaranteed to always return identical results. For example,
MException.last updates its error status only on uncaught errors,
where lasterror can update its error status on any error, whether it
is caught or not.
See Also
dbstack | lastwarn | rethrow | assert | error | MException | MException.last | try, catch
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)