Does Error Message Depend on Whether or Not Function is Executed Inside Try/Catch?

3 ビュー (過去 30 日間)
Paul 2023 年 8 月 13 日
回答済み: Paul 2023 年 8 月 24 日
This call to diff resolves to symbolic diff
syms s
diff(@(s) 1/(s+1),s)
ans = 
which diff(@(s) 1/(s+1),s)
/MATLAB/toolbox/symbolic/symbolic/@sym/diff.m % sym method
I guess the parser sees that the first input is an anonymous function, for which there is no diff method, then goes to second argument, decides to call sym->diff, and that works becuase the function_handle can be promoted to a sym expression.
This call to diff throws an error.
B = BasicClass;
catch ME
ans = 'Undefined function 'diff' for input arguments of type 'BasicClass'.'
I don't understand the error message, because based on the pervious result I would have expected sym->diff to be called, and then the error message would be something like 'can't convert BasicClass object to sym' or something like that. I've read over the doc pages, and still don't understand why it doesn't try to call sym->diff and instead looks for a BasicClass method called diff.
As expected, which doesn't find anything for that call.
which diff(BasicClass,s)
'diff(BasicClass,s)' not found.
But, making the same call at the command prompt generates a different error:
Incorrect number or types of inputs or outputs for function 'diff'.
Why is this error different than that generated inside the try/catch, and to which diff function is this error message referring?
  2 件のコメント
Walter Roberson
Walter Roberson 2023 年 8 月 14 日
Interesting. I agree, the resolution does appear to differ depending on whether you are inside a try/catch .
Paul 2023 年 8 月 14 日
Never occurred to me that the resolution would be different, but now that you say that I wouldn't be surprised. I tried to step into that last call to diff in the debuggger to see where the error message was coming from, but that didn't work. Maybe it resolved to some built-in method that can't be debugged.
Also, FWIW, the error message from the command prompt is different in 2022a:
>> diff(BasicClass,s)
Check for incorrect argument data type or missing argument in call to function 'diff'.



Paul 2023 年 8 月 24 日
I put in a service request to Tech Support on this question. Their answer was that the error that's returned to the user does, or can, in fact, depend on whether or not the offending call is inside a try/catch block. So the answer to the title question is: yes, the error message can depend on if the error occurs in a try/catch or not.
If inside a try/catch block, the error message that is caught is the, actual, original error message.
However, if the error is not caught, then Matlab can take additional action and return a different error message to the screen, presumably in an attempt be more helpful.
In this particular example, my opinion is that the additinal action was actually unhelpful.

その他の回答 (0 件)


Help Center および File ExchangeNumber Theory についてさらに検索




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by