gpt4 book ai didi

matlab - 尝试/捕获并使用空字符串错误

转载 作者:行者123 更新时间:2023-12-03 08:05:33 32 4
gpt4 key购买 nike

我正在使用其他人的代码,但我不熟悉try/catch,因此我举了一个类似的小例子。在第11行上,如果我写了error(''),似乎没有发现错误并增加了索引j。但是,编写error(' ')error('bad!')确实可以。

那么使用空字符串出现错误会忽略该错误,还是我做错了什么?

% Just a file to understand the Matlab command try/catch

M = 3;
j = 1;
k = [Inf, 5, 4];

while M>0
try
M = M-1
u = k(j)
if (isinf(u)||isnan(u)), error(''), end;
catch
j = j+1
end
end

最佳答案

是的,error('')以及error([])error(struct([]))都不会真正显示错误消息并中止运行代码。我个人认为在任何实际代码中使用error的单字符串参数版本都是不好的做法。在为函数编写错误时,例如,应同时使用'MSGID''ERRMSG'

error('FunctionName:SubFunctionName:ErrorMSGID','Error message to be printed.')

或者,您可以将 MException 对象与 throw rethrow throwAsCaller 结合使用,从而可以重用错误信息。更多 here

关于matlab - 尝试/捕获并使用空字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20383140/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com