0

I have a problem with programming. I want to handle error since it would be work well. For example in following example i will be increase index to positive value till there is no error anymore :

i=-10;
try
result=a[i];
    disp('success');
end
catch
i=i+1;
end

It is just simple example to express my problem. I will really appreciate, if anyone could help me.

1
  • 1
    Don't use try catch for flow control. Just use if statement to check if i < 1. Commented Aug 12, 2013 at 2:36

2 Answers 2

1

You've got one superfluous end keyword just before the catch-line.

It should be

result = []
i = -10;
while isempty(result)
   try
      result = a(i);
      disp('success')
   catch
      i = i+1;
   end
end

I've also put a loop around it, to make i actually be increased. As an aside, a[i] isn't a valid expression in matlab, there is only a(i) or a{i} (in case a is a cell-array).

Sign up to request clarification or add additional context in comments.

Comments

0

I am not too sure what you are asking, but I looked up the try/catch page on Matlab and it seems to be helpful. Try/catch helps you deal with errors. For instance, the example they gave about the combinations really highlights its usage. From what I see you are using try/catch appropriately, provided you have the vector a declared.

1 Comment

Thanks for your respond. I want to change special parameters if error will be happen. Suppose that you set special parameters (in some situation the parameter is not work well and caused error ), For this reason, it will be needed to change parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.