开发者

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 r开发者_StackOverflow中文版ead 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.


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.


I search for that, and I did not find anything... The only thing I see it could help you, would be analyse the source code of imread, which I don't think it's possible. However, you could always try to see the source code of the same function in Octave, since it almost the same (I suppose).


If you're willing to enumerate files to generate your own reference, try

grep -r "MATLAB:" <matlab root>

you'll get a long list...but it appears errors are either thrown by error() or mexErrMsgIdAndTxt. Those plus function names let you be more specific. Pretty slow though.

grep will also note that some binary files match. Feed it -a and it will be able to pull out the error messages from many of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜