【发布时间】:2012-05-03 10:08:09
【问题描述】:
据我所知,在 Matlab 中,没有有条件地捕获异常的好方法(如果我错了,请纠正我)。唯一的方法是捕获异常,检查标识符,如果无法处理此特定错误,则重新抛出错误。这是可以接受的,虽然不方便。但是,当我使用 Matlabs dbstop if error 时,我最终会出现在 ME.rethrow() 行。然后我无法dbup 回到导致原始错误的地方。
function test_excc
try
sub_test()
catch ME
if strcmp(ME.identifier, 'test:notsobad')
fprintf(1, 'Fine\n');
else
ME.rethrow();
end
end
end
function sub_test
sub_sub_test();
end
function sub_sub_test()
if rand>0.5
error('test:error', 'Noooo!');
else
error('test:notsobad', 'That''OK');
end
end
示例用法:
>> test_excc()
Error using test_excc>sub_sub_test (line 21)
Noooo!
Error in test_excc>sub_test (line 16)
sub_sub_test();
Error in test_excc (line 4)
sub_test()
9 ME.rethrow();
K>> dbstack
> In test_excc at 9
虽然 Matlab 桌面环境将整个堆栈跟踪打印回sub_sub_test,但调试器并没有让我能够上堆栈跟踪并在此函数内部进行调试。
我知道dbstop if caught error。但是,这将调试到 any 捕获的错误,如果软件大量使用异常,则可能会出现很多错误。我只想在未捕获的错误上停止,但我想在产生错误的地方停止——而不是在重新抛出错误的地方。
我的问题:
- 在 Matlab 中,如何有条件地捕获错误(基于错误标识符)并调试到最初引发错误的地方?
【问题讨论】:
标签: debugging matlab error-handling