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

3 次查看(过去 30 天)
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;
try
diff(BasicClass,s)
catch ME
ME.message
end
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:
diff(BasicClass,s)
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
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by