MATLAB: Do I get unexpected behavior when I use LASTERR command after using CATCH command with an identifier in MATLAB 7.6 (R2008a)

errorlastlasterrorMATLAB

I am trying to use the TRY/CATCH blocks with an identifier and use the LASTERR command in the CATCH block. In such a case, the LASTERR seems to grab the error that occured before the TRY/CATCH blocks. An example code situation would be to first execute :
imread('foo')
Now, execute the following:
try
fdfs
catch ME
lasterr
end
I would expect the LASTERR command to grab the error due to the 'fdfs' statement, but it seems to throw to IMREAD error that occured before executing this TRY/CATCH block.

Best Answer

This is the intended behavior of the LASTERR command.
When the CATCH command is used with an identifier, it does not change the status of the global LASTERR. Hence, we should use the identifier to check the error occured and not the LASTERR command.
Hence, the code should be as follows:
try
fdfs
catch ME
ME.message
end