Why does the Code Analyzer flag this as an error?
8 ビュー (過去 30 日間)
古いコメントを表示
Why does the Code Analyzer flag this code as an error? It runs fine:

try
error("It's bad")
catch ME
warning('Error in code: %s', ME.message);
end
0 件のコメント
採用された回答
その他の回答 (1 件)
dpb
2025 年 2 月 27 日
移動済み: dpb
2025 年 2 月 27 日
Having warning() inside the catch clause is the problem -- the catch clause is executed only when an error condition exists and mixing that up with trying to issue a warning is sorta' like double jeopardy and mlint doesn't like it...
Just output any additional message with disp or fprintf, don't try to throw a warning there...
try
error("It's bad")
catch ME
fprintf('Error in code: \n%s\n',ME.message);
end
shows no error or warning.
2 件のコメント
dpb
2025 年 2 月 27 日
編集済み: dpb
2025 年 2 月 27 日
The suggested solution doesn't work(*) when the warning is inside the catch block, either...I believe the authors of mlint never thought of nor tested the case.
It's probably worth an enhancement/bug report...
(*) Actually, on further exploration using the other format string argument does convert the mlint error into a warning although it may take moving around in the file some first before the red line is replaced by the orange one. You then get a warning that the number of output items may not match the format specification. Either way, there isn't an actual syntax error in the warning statement per se, just a picky mlint false positive, I think.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
