4

Some part of my source code are nested inside try statements in order to handle some run time errors, at the same time each and every line must be tried to execute even if the previous line is not executed because of run time error .

currently my code looks like this

try

  try
  //statement1
  except 
  end;

  try
  //statement2
  except 
  end;

  try
  //statement3
  except 
  end;

finally
//something
end;

I am very sure going in the wrong way ,even if the final out put is working well, I have to do this for dozens of lines.

Is there any better way to implement this

1 Answer 1

5

If you want each statement to execute then you have to write it the way you have done. Note that in that case may not need the try/finally because you are swallowing all the exceptions.

However, the code does look a bit odd to me. I wonder if you really need each and every statement to execute. Normally you would write:

try
  statement1;
  statement2;
  statement3;
except
  //handle exceptions
end;

Then, if there is an exception in statement1, the other two lines will not execute.

However, it would be even more common not to handle exceptions at all and let them float up to some higher level handler. If you are making routine logic decisions using exceptions then that would be considered bad practice.

I think it would be helpful for you to publish some of your code that handles the exceptions and some details of what exceptions you are expecting to occur. Then we could give you some more specific advice.

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

1 Comment

+1 - this has a smell of "you're doing it wrong." Maybe he's porting VB code that began with "on error resume next"

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.