If I run the following foo.m file with run('foo.m'):
try
disp(r3)
catch ME
disp('Exception handling.')
end
I correctly get:
Exception handling.
However if I replace disp(r3) by disp('foo' 1) then I get:
Error: File: C:\Users\Pedro\Desktop\foo.m Line: 23 Column: 16
Unexpected MATLAB expression.
Error in run (line 96)
evalin('caller', [script ';']);
Why am I not catching this error with catch ME? How can I catch it?
Having a syntax error in your file, not a single line of code is broken, the full file is broken because it can not be parsed. Matlab will refuse to "understand" any code you write in your foo.m, including your try/catch. You have to write your try/catch into another function which calls the foo.m
try
foo()
catch ME
disp('Exception handling.')
end
As the matlab interpreter will highlight all syntax errors to you without running the code, you typically don't need to check for syntax errors on runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With