MATLAB 消息 ID 的最佳实践?
创建 MATLAB 异常(MException 对象)或打印警告或错误消息时,MATLAB 允许您提供定义要抛出的异常的消息 ID。
消息ID的格式为:
component:mnemonic
例如,MATLAB自己的未定义变量消息ID为:
MATLAB:dispatcher:nameConflict
那么当您在自己的代码中使用异常时,您使用什么消息ID?您是否重用 MATLAB 的默认值?自己编吗?组件和助记符字符串使用什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常遵循这种错误(或警告)模式 消息标识符,其中括号中的内容可能存在也可能不存在:
组成部分是:
className
:类的名称,如果发生错误的函数是方法/构造函数。parentFunction
:如果发生错误的函数是 m 文件中的子函数 或 嵌套函数,这将是 主 m 文件函数 或嵌套函数的父函数。因此,您可以有多个parentFunction
组件。functionWhereErrorOccurs
:该组件的名称非常不言自明。 ;)descriptiveMnemonic
:我强调描述性。例如,inputError
并没有真正告诉我任何信息,但是notEnoughInputs
清楚地表明我没有传递足够的参数。我总是使用小驼峰式大小写作为助记符,其中单词的第一个字母除了第一个单词外都大写。className
和parentFunction
组件可以被认为有些多余,因为stack 属性 .mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bq9l448-1.html#bq9l5ac" rel="noreferrer">MException
类 已标识父级的完整路径m 文件和错误的行号。但是,消息的目的之一标识符的优点是它允许您唯一识别错误,而不仅仅是为了追查错误的来源。假设您有一个函数
myFcn
和一个重载myFcn
的类myClass
。如果您将第一个错误消息标识符设置为myFcn:maxIterationsReached
并将第二个错误消息标识符设置为myClass:myFcn:maxIterationsReached
,这将允许您,例如,使用 DBSTOP 设置一个断点来停止仅当此错误由myClass\myFcn
而不是myFcn
产生时才执行。同样,唯一的警告消息标识符很有用,因为您可以专门选择忽略来自特定函数的警告,同时显示其他函数。此外,您还可以在标识符中包含组件,指示发生错误的函数位于 包文件夹 或 私人文件夹(但这可能会导致标识符相当长)。
I generally follow this pattern for error (or warning) message identifiers, where things in parentheses may or may not be present:
The components are:
className
: The name of the class, if the function where the error occurs is a method/constructor.parentFunction
: If the function where the error occurs is a subfunction in an m-file or a nested function, this would be the primary m-file function or the parent of the nested function, respectively. You could therefore have multipleparentFunction
components.functionWhereErrorOccurs
: The name of this component is pretty self-explanatory. ;)descriptiveMnemonic
: I stress descriptive. For exampleinputError
doesn't really tell me anything, butnotEnoughInputs
makes it clear that I didn't pass enough arguments. I always use lower camel case for the mnemonic, where the first letter of a word is capitalized except for the very first word.The
className
andparentFunction
components could be considered somewhat redundant, since thestack
property of theMException
class already identifies a complete path to the parent m-file and the line number of the error. However, one of the purposes of a message identifier is that it allows you to uniquely identify an error for purposes other than just hunting down the source of the error.Let's say you have a function
myFcn
and a classmyClass
that overloadsmyFcn
. If you make an error message identifier for the first one bemyFcn:maxIterationsReached
and an error message identifier for the second one bemyClass:myFcn:maxIterationsReached
, this would allow you to, for example, set a breakpoint with DBSTOP that halts execution only when this error is produced bymyClass\myFcn
and notmyFcn
. Likewise, unique warning message identifiers are useful in that you can specifically choose to ignore warnings from specific functions while letting others be displayed.Additionally, you could also include components in the identifier indicating that the function where the error occurs is located in a package folder or a private folder (but this might make for a rather long identifier).
在我的工作中,我使用 YMA:(mainFunctionName):(descriptiveMnemonic),其中 YMA 只是我的姓名缩写。例如,我的 UIInspect 实用程序 的 ID 类似于
YMA:uiinspect:XXX
。In my work I use YMA:(mainFunctionName):(descriptiveMnemonic), where YMA are simply my initials. For example, all the warnings and errors invoked in my UIInspect utility have IDs similar to
YMA:uiinspect:XXX
.