Why does the Code Analyzer flag this as an error?

6 visualizaciones (últimos 30 días)
Matt J
Matt J el 26 de Feb. de 2025
Comentada: dpb el 6 de Mzo. de 2025
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
Warning: Error in code: It's bad

Respuesta aceptada

Matt J
Matt J el 6 de Mzo. de 2025
Mathworks Support has classified it as a bug.
  1 comentario
dpb
dpb el 6 de Mzo. de 2025
That's pretty-much what my conclusion was although I said mlint was too picky... :)

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 27 de Feb. de 2025
Movida: dpb el 27 de Feb. de 2025
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 comentarios
Matt J
Matt J el 27 de Feb. de 2025
Editada: Matt J el 27 de Feb. de 2025
That does make a certain amount of sense, but the explanation given by the Code Analyzer doesn't resemble that:
dpb
dpb el 27 de Feb. de 2025
Editada: dpb el 27 de Feb. de 2025
Yeah, @Matt J, I thought about addressing that but decided to not touch it. :)
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by