这就是为什么我编写了一个包装函数来尝试处理错误的原因,以便在丢帧的情况下,只需再次捕获它,而不会中止正在运行的代码:
function img = getsnapshotfcn(obj, maxRetries, pausing)
% Default args
if(nargin < 3)
pausing = 4; % in sec
end
if(nargin < 2)
maxRetries = 20;
end
% Try to capture snapshot...
i = 0;
while(1)
i = i+1;
try
img = getsnapshot(obj);
% success? -> break loop
break;
catch err
% Pause shortly before retry
pause(pausing);
end
% maxRetries reached? -> error
if(i > maxRetries)
error('snapshot:maxRetriesReached', 'blaaa...');
end
end
end
现在当使用
getsnapshotfcn()
时,有时仍然仍然会在控制台中显示上面发布的错误消息,我想知道为什么。我没想到错误会消失(并且请不要对此帮忙,我已经知道我需要设置已经完成的PacketDelay
,但是在极少数情况下,即使在快速CPU上运行,它仍然会发生)我希望它不会像希望通过try-catch
捕获那样被显示。仍在控制台中显示的情况可能是什么?
提前非常感谢您。
最佳答案
我可以通过在MATLAB中使用evalc
来解决它
evalc('img = getsnapshot(obj);')
代替
img = getsnapshot(obj);
在上面的代码中:-)
关于matlab - MATLAB : Getting error message while using try/catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299609/