0

I have a While loop, and a try catch block within it. How can i add a break statement in the catch statement to break the while loop. I don't want to use a DO-WHILE loop because i just have a few minutes to submit my code, and i don't want to break the program by making the change. But, later on i will consider changing the code using a DO-WHILE statement.

while (true) {
try {
// something here
} catch (Exception e) {

// I need to break the forever while loop here
}
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Illep
  • 16,375
  • 46
  • 171
  • 302

8 Answers8

8
try {
// something here
    while (true) {
    // While body here.
    }
} catch (Exception e) {

// I need to break the forever while loop here
}
} 

You can move the while loop inside the try catch body. This will act EXACTLY the same programmatically, but all errors will be caught and there's no need for a do-while. This looks a lot nicer, and makes more semantic sense.

Moreover, just add the word break; in the catch block, and it will stop the running of your loop.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • 2
    Maybe he doesn't want the while loop in the try-catch block. For example, if he only needs try/catch for some kind of read/write/database access. In that case, adding break in the catch block sounds like the best solution. – ktm5124 Mar 12 '13 at 07:53
  • 1
    Furthermore, a while(true) loop within a try-catch block does not look nice to me whichever way you spin it :-) – ktm5124 Mar 12 '13 at 07:54
  • It won't 'act exactly the same programmatically'. It depends entirely on what's inside the `catch` block. And no code motion is required to solve this non-problem. – user207421 Mar 12 '13 at 09:33
3

Just put a break statement in the catch block

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
0

Unless you have another loop in the catch clause (which would be weird), just use break;.

try-catch is not a loop, so the break affects the first containing loop, which seems to be the while.

0

It would work.

But what you should do is leaving the loop because you had an exception like:

try {
    while (true) {
        // something here
    }
} catch (Exception e) {
    // Do whatever in the catch
} 
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

You can introduce a boolean variable that is initially true in your while loop, and that you set to false anywhere in either your try or catch statement. This way, you can break the loop you have given even if there are nested loops inside it.

boolean notDone = true;
while(notDone) {
try {
// something here
} catch (Exception e) {
// I need to break the forever while loop here
notDone = false;
}

You can use off course use the inverted version, using a "done" boolean instead. This causes you to need to call an invert inside the while-loop for each iteration. It's a trade-off between minor-performance and increased code-readability.

Xilconic
  • 3,785
  • 4
  • 26
  • 35
0

Well there are multiple approaches

Approach1 - by reversing the While Condition variable

boolean notDone = true;
while(notDone) {
    try {
    // something here
    } catch (Exception e) {             
        notDone = false;
    }
}

Approach2 - Using break statement

while(notDone) {
    try {
    // something here
    } catch (Exception e) {
        break;
    }
}

Approach 3 - moving the catch block outside of while

try {
        while(notDone) {

        }       
    } catch (Exception e) {

    }       
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
0

How can i add a break statement in the catch statement to break the while loop

Just do exactly that. Add a break statement in the catch statement. It will break out of the while loop.

Not a real question.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

Just name your loop and break it
For example

Lable:
while (true) {
    try {
        // something here

    } catch (Exception e) {
        //  I need to break the forever while loop here
        break Lable;
    }
}
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88