Programming |
Using Message Identifiers with lasterr
One use of message identifiers is to enable the lasterr
and lasterror
functions to better identify the source of an error. These functions return a message identifier, and you can use the combination of component
and mnemonic
parts of that identifier to identify both a broad category of errors and a specific instance within that category, respectively.
The first step in using this feature is to determine which of your error messages need this type of identification and then tag each with an identifier. You do this by specifying a message identifier argument (msg_id
, below) along with the error message in your calls to error
. Either form shown below is acceptable. The latter form uses formatting conversion characters as described in Formatted Message Strings.
Note
When you specify more than one input argument with error , MATLAB treats the errormsg string as if it were a formatted_errormsg . This is explained in Formatted String Conversion.
|
The message identifier must be the first argument and must be formatted according to the rules covered in Message Identifiers.
The message identifier is not a required argument for error
. If you don't need to return this type of information with lasterr
, then you can omit the msg_id
argument from the error
function syntax shown previously:
Returning a Message Identifier from lasterr
Use lasterr
with one output to return just the string holding the error message from the most recently generated error.
Use lasterr
with two outputs to return both error message string and the message identifier for that error.
The following example performs an operation in the try
segment of the try-catch
block that results in an error. The first line of the catch
segment retrieves both the error message string and message identifier for the error. The example then responds to the error in a manner that depends on the identifier returned.
try [d, x] = readimage(imageFile); catch [errmsg, msg_id] = lasterr; switch (lower(msg_id)) case 'matlab:nosuchfile' error('File "%s" does not exist.', filename); case 'myfileio:noaccess' error(['Can''t open file "%s" for reading\n', ... 'You may not have read permission.'], filename); case 'myfileio:invformat' error('Unable to determine the file format.'); end end
If the last error has no message identifier tag associated with it, then MATLAB returns an empty string in the second output argument.
error('This error has no message identifier.'); ??? This error has no message identifier. [errstr, msgid] = lasterr errstr = This error has no message identifier. msgid = ''
Note
Both lasterr and lasterror return a message identifier. Although this section discusses only lasterr , you can use lasterror in the same way.
|
Inputs to lasterr
In addition to returning information about the last error, lasterr
also accepts inputs that modify the MATLAB copy of the last error. Use the command format shown below to change the error message string and message identifier returned by subsequent invocations of lasterr
.
All lasterr
input arguments are optional, but if you specify both an error message and message identifier input, they must appear in the order shown above.
Message Identifiers | Warnings |
© 1994-2005 The MathWorks, Inc.