For example, I want to catch a couldn't-read-a-file-at-that-path exception from imread(). I can do this.
imagePath = 'a_picture.jpg';
try
im = imread(imagePath);
catch exception
if strcmp(exception.identifier, 'MATLAB:imread:fileOpen')
fprintf('Couldn''t open %s.\n', imagePath);
im = [];
else
fprintf('Unexpected error (%s): %s\n', ...
exception.identifier, exception.message);
throw(exception);
end
end
But the only ways I know to discover the magic string to compare with ('MATLAB:imread:fileOpen' in this case), are:
Cause the error, catch the exception, and look at the identifier. But it would take a long time to do this right. For example, does Matlab use a different exception identifier if the file exists but is not actually an image file? How about if it exists but I don't have read permission? What if it's a directory?
Look at the source code. imread() is written in Matlab, so this is possible, but it wouldn't be for other functions. And of course imread() calls other functions that are not written in Matlab, and exceptions could bubble up from them.
Is there any authoritative way for me to know all the exceptions imread() can throw? I'm hoping this is in the documentation somewhere, but I can't find it.