Is there any other way than try & catch to handle error in matlab?

I have a code that is executed to give a result. But it is possible that it would not execute due to some alteration done to the code before execution. How do I control the error that is expected if the code is not running. For example:
result = compute(x, y)
The value of result can be a real number or the compute function does not complete the execution. How do I assign a particular value (say -1) to result if the function fails to execute.

Antworten (2)

Walter Roberson
Walter Roberson am 6 Aug. 2017
Bearbeitet: Walter Roberson am 6 Aug. 2017
Do not have your function call
result = compute(x, y)
instead have it call
clear compute
result = test_function(@compute, substitute_value, x, y);
where
function output = test_function(fh, substitute_value, varargin)
try
output = feval(fh, varargin{:});
catch
output = substitute_value;
end
Now, as long as the function does not exit MATLAB or crash MATLAB, any error will be caught.
As long as only the function compute changes, and not the function test_function, then you should be fine.
Note: the above code does assume that compute() is not a function handle.
Jan
Jan am 6 Aug. 2017
You cannot predict reliably, if the function fails or not without running it. Such a test is impossible even in theory (search for "Turing halting problem").
You can check if the inputs cause problems inside the function also:
function r = compute(x, y)
if y == 0
r = NaN;
else
r = x / y;
end
But a try catch block is the perfect solution for catching errors, if you cannot modify the function accordingly. Why do you want to avoid it?

4 Kommentare

Thanks for your contribution. Actually, I am working on mutation testing whereby a function is altered and a test is generated to see if the result of the altered function would be different from the result of the original function. If the results are different, then the alteration is significant else otherwise. One of the alterations is statement deletion. So even if I include try-catch block, one of the components of the try-catch block could be the statement to be deleted as a result of random deletion that is applied; which would result in the error we are trying to avoid. This is why I need a way to detect if the code gives error or it is executed successfully without changing anything in the original/altered code. I believe the intention is clear now. If it is not clear, pls let me know. Thanks!
The posted link is dead.
Do you modify M-code dynamically? And dare to run it? Bold.
quit = 17;
plot(quit, 3);
Now remove the first line and run the code again. Even try/catch in the caller will not help. Calling the code in a sandbox, e.g. as an additional Matlab engine, cannot solve problems like:
s = '*.*';
s = 'C:\Temp\file.mat'; % <- removed randomly
delete(s);
My impression is, that the problems are much more serious than a removed try.
Yes, you are right. I have modified it.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by