Purpose of error identifiers

I am curious about the practical uses of error identifiers, for example the 'identifier' property in MException objects.
From the documentation (https://uk.mathworks.com/help/matlab/matlab_prog/capture-information-about-errors.html#bq9tdlq-1), I gather that the identifiers are tags for error statements that make them unique. The docs go on to say that the the identifiers can help better identify the error source and also be used to control the error display. The examples that follow however just show how identifiers are created and extracted, so I struggle to see how it can be useful over say, just returning the 'message' property itself.
Why make error statements unique? What is the utility of the identifiers?

 Accepted Answer

Walter Roberson
Walter Roberson on 11 May 2019
It is common for the exact spelling and phrasing and formatting of error messages to evolve. When you go to try/catch an error it is inconvenient to have to update all of the various places that have a particular exact spelling and phrasing to be able to match the changes. Instead it is more convenient to use a token that does not change to represent the error for try/catch purposes, permitting the spelling and phrasing to be changed in just one place.
In particular, using a token to stand in is a practical necessity when you make the program multilingual. When you use a token (error identifier) you can try/catch on just one short representation of the condition and then program in "message catalogs" that hold the translation to a variety of human languages.

4 Comments

As a concrete example where what Walter described is useful, you can use verifyError to perform negative testing of your code (checking for a particular error being thrown) without having to update your test whenever you tweak the error message (and without having to worry about your test breaking when you send your source code and test code to a colleague in a different country.)
In addition to what Walter said, if you want to debug your code (which may consist of many lines and take a long time to run) to stop when a particular error occurs, you could use dbstop if error. If the error is caught by a try / catch block you'd need to use dbstop if caught error instead. But if your code has a lot of try / catch blocks, it would be useful to stop only in the one that caught the error in which you're interested. In that case you can stop only caught errors with a particular identifier, as per the line of code at the end of the Error Breakpoints section on this documentation page.
You can also check in your code if the MException a particular try / catch block caught has a particular identifier, and if that's an error that you know you can safely handle or ignore, you can "customize" your catch. See the examples on the try / catch documentation page for two such customizations.
Is there any way to build my own message catalog? For example,
error(message('optimlib:lsqcurvefit:InputArg'))
would issue an error says "The input to LSQCURVEFIT should be either a structure with valid fields or consist of at least four arguments."
How can I store the message in my own toolbox? For example,
error(message('MyToolbox:MyFunction:InputArg'))
I would like the above statement to issue an error says "The input should be double for MyFucntion in MyToolbox".
I do not know if there is a better way to create your own catalog, but the implication is that you could create a matlabroot resources/MyToolbox folder to put the catalogs into.
@Walter Roberson Thanks, Walter. I've read that answer before asking here. I'll try it.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming Utilities in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!