Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any authoritative reference on what exceptions can be thrown by Matlab's built-in functions?

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:

  1. 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?

  2. 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.

like image 398
Tom Future Avatar asked Jan 19 '11 21:01

Tom Future


1 Answers

No, there is not.

The problem is that even if there would be an authoritative reference on what a given function in MatLab throws, it could change from version to version. So even if you could do it, you probably should not.

I would recommend only checking for the ones that you know you can handle, and generate some generic errors for others (or reuse what MatLab gives you).


Some comments based on other languages/frameworks:

In .NET, the only list of exceptions that can be thrown from a method, is in documentation, and is not liked to the source code. These are often out of date, invalid, and incomplete.

In Java, you can specify what exception is thrown form what method. This is then verified by the compiler, and therefore an authoritative reference can be built by the compiler. MatLab lacks such a feature, therefore the best you can do is searching as outlined in other answers.

like image 174
earlNameless Avatar answered Nov 03 '22 19:11

earlNameless