2

I have a mainloop within my C++ program. I also have a function for exception handling.

It's syntax looks like this:

void handleEx(int errorCode) 

After calling this function I always use continue; to restart my loop, so it looks like this:

if(/*exception occured*/) { handleEx(5); continue; } 

Is it possible to put the continue; inside the function so I wouldn't have to rewrite that command and the {}?

7
  • 1
    Short answer: no. Not exactly like you're asking, anyway. Commented Jul 16, 2015 at 15:58
  • This is impossible to answer (provide a better example) Commented Jul 16, 2015 at 15:58
  • 1
    You don't want your continue inside of your function call. Putting it this way makes it readable to anyone what is happening when an exception occurs. If you put the continue inside of the function, and someone doesn't look at the function, they won't understand that you are skipping the rest of the loop when an exception is raised. Always keep it simple. Commented Jul 16, 2015 at 16:03
  • Also, in general, don't worry about having extra brackets or how much space you use. The most common mistake new programmers make is trying to make everything small and neat. Make everything readable and use standard conventions when possible and your life will be much easier in the long run. Commented Jul 16, 2015 at 16:05
  • Thanks for your help! What now, though? I can't accept any answer, because the comments ansered my question Commented Jul 16, 2015 at 16:08

1 Answer 1

2

You should have the function return a bool so your function header will look like bool handleEx(int errorCode); and then within the while loop you can do:

while(something) { //... if(handleEx(myError)) continue; //... } 
Sign up to request clarification or add additional context in comments.

3 Comments

I always want to continue; after this funtion call, so I'd have a second if in there, which is always true.
@kim366 What do you mean? And what good would an if be if it was always true?
@scohe001 That's the point. You're saying handleEx should return true if the rest of the iteration should be skipped. But OP is saying that it should always be skipped when handleEx is called. So it'd always return true and the if would always go in the then-branch. And that makes no sense. So it makes no sense for the function to return a boolean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.