Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab try/catch并使用空字符串出错_Matlab_Error Handling_Try Catch - Fatal编程技术网

Matlab try/catch并使用空字符串出错

Matlab try/catch并使用空字符串出错,matlab,error-handling,try-catch,Matlab,Error Handling,Try Catch,我正在使用其他人的代码,我不熟悉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

我正在使用其他人的代码,我不熟悉
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
如果你看一下文档,你可以举个例子

您还需要为您的代码添加以下内容:

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
        disp('I catch an error!');
        j = j+1
    end
end
因为如果你的代码中没有错误,它就永远不会被捕获。因此,包括
错误(“”),它只是说,去执行catch中的语句

但您只需将
error()
替换为以下语句即可修改代码:

while M>0
        M = M-1
        u = k(j)
        if (isinf(u)||isnan(u)), j = j+1, end;
end
编辑

如果您查看文档,可以找到以下内容:

%   ERROR(MSGSTRUCT) reports the error using fields stored in the scalar
%   structure MSGSTRUCT. This structure can contain these fields:
%
%       message    - Error message string
%       identifier - See MESSAGE IDENTIFIERS, below
%       stack      - Struct similar to the output of the DBSTACK function
%  
%   If MSGSTRUCT is an empty structure, no action is taken and ERROR
%   returns without exiting the program. If you do not specify the
%   stack, the ERROR function determines it from the current file and line.
因此,正如您所看到的,没有采取任何行动。什么都没有,所以不要得到任何信息。

如果你看了文档,你可以举个例子

您还需要为您的代码添加以下内容:

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
        disp('I catch an error!');
        j = j+1
    end
end
因为如果你的代码中没有错误,它就永远不会被捕获。因此,包括
错误(“”),它只是说,去执行catch中的语句

但您只需将
error()
替换为以下语句即可修改代码:

while M>0
        M = M-1
        u = k(j)
        if (isinf(u)||isnan(u)), j = j+1, end;
end
编辑

如果您查看文档,可以找到以下内容:

%   ERROR(MSGSTRUCT) reports the error using fields stored in the scalar
%   structure MSGSTRUCT. This structure can contain these fields:
%
%       message    - Error message string
%       identifier - See MESSAGE IDENTIFIERS, below
%       stack      - Struct similar to the output of the DBSTACK function
%  
%   If MSGSTRUCT is an empty structure, no action is taken and ERROR
%   returns without exiting the program. If you do not specify the
%   stack, the ERROR function determines it from the current file and line.
因此,正如您所看到的,没有采取任何行动。什么都没有,所以catch不会得到任何信息。

是的,
错误(“”)
错误([])
错误(结构([])
都不会显示错误消息并中止运行代码。我个人认为使用单字符串参数版本<代码>错误<代码>在任何实际代码中都是不好的做法。在为函数编写错误时,应始终使用
'MSGID'
'ERRMSG'
,例如

error('FunctionName:SubFunctionName:ErrorMSGID','Error message to be printed.')
或者,您可以将对象与和结合使用,这允许您重用错误信息。更多。

是,
错误(“”)
错误([])
错误(结构([])
都不会实际显示错误消息并中止运行代码。我个人认为使用单字符串参数版本<代码>错误<代码>在任何实际代码中都是不好的做法。在为函数编写错误时,应始终使用
'MSGID'
'ERRMSG'
,例如

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

或者,您可以将对象与和结合使用,这允许您重用错误信息。更多。

这很奇怪,但它在中,对于
错误('msgString')
语法:

所有字符串输入参数必须用单引号括起来。如果
msgString
是空字符串,则错误命令无效

类似地,如果使用
错误(msgStruct)
语法:

如果
msgStruct
是空结构,则不执行任何操作,并且在不退出函数的情况下返回错误


这很奇怪,但它在中,对于
错误('msgString')
语法:

所有字符串输入参数必须用单引号括起来。如果
msgString
是空字符串,则错误命令无效

类似地,如果使用
错误(msgStruct)
语法:

如果
msgStruct
是空结构,则不执行任何操作,并且在不退出函数的情况下返回错误


不知道你为什么需要它,但下面是它的工作原理

error
函数不会以空字符串或空向量([])作为参数抛出错误

s = struct();
error(s)
如果根本不指定参数,则
error
函数本身会生成错误“参数不足”。所以它会被抓住的

另一种方法是指定一个空结构作为参数

s = struct();
error(s)

在这种情况下,将生成错误,但代码不会停止,并且在一般流程中,您不会听到蜂鸣音。在你的情况下,它应该去捕捉

不确定您为什么需要它,但下面是它的工作原理

error
函数不会以空字符串或空向量([])作为参数抛出错误

s = struct();
error(s)
如果根本不指定参数,则
error
函数本身会生成错误“参数不足”。所以它会被抓住的

另一种方法是指定一个空结构作为参数

s = struct();
error(s)

在这种情况下,将生成错误,但代码不会停止,并且在一般流程中,您不会听到蜂鸣音。在你的情况下,它应该去捕捉

我的问题是关于使用error(“”)和error('nonempty string')。除非错误中有一个非空字符串,否则top case仍然不会进入catch(对我来说)。我想知道,让错误被忽略是否是错误(“”)的标准。我喜欢第二个例子。也许我必须编辑代码才能像这样工作,但现在我正试图理解一个比我的示例更复杂(但类似)的代码。@Catalina,噢!你说得对。我从来没有尝试过。因为通常当我抛出错误时,我使用消息或者像这样使用它
error()。让我看看。我会给出另一个答案。我的问题是关于使用error(“”)和error('nonempty string')。除非错误中有一个非空字符串,否则top case仍然不会进入catch(对我来说)。我想知道,让错误被忽略是否是错误(“”)的标准。我喜欢第二个例子。也许我必须编辑代码才能像这样工作,但现在我正试图理解一个比我的示例更复杂(但类似)的代码。@Catalina,噢!你说得对。我从来没有尝试过。因为通常当我抛出错误时,我使用消息或者像这样使用它
error()。让我哈