Reputation: 89
So here's some code from my application:
public void run() throws myException{
boolean gameOver = false;
while(!gameOver){
//do stuff, and eventually make gameOver true to end execution
...
Now heres the deal, I want to remove the boolean attribute to use a user-defined exception to end the execution. I was thinking of checking if this exception had been thrown or not in the while condition, to keep looping as long as it has not been thrown.
Something along the lines of
while(myException.notThrown){
Can this be done?
Upvotes: 2
Views: 1648
Reputation: 10064
Yes, it is possible. But using exceptions for flow control is generally considered an anti-pattern; it makes harder to read and debug code.
Upvotes: 0
Reputation: 1335
You could use an endless while together with an break. You would break the while if the exception occurs.
while (true) {
try {
...
} catch (YourException e) {
break;
}
}
Upvotes: 0
Reputation: 2668
but why? in case you really need it, you can make an infinite loop to do it, you can do something like that
while (true) {
try {
// some code to throw an exception
} catch(Exception e) {
e.printStackTrace();
break;
}
}
UPDATE you can make an inner loop into exception
try {
while (true) {
// some code to throw an exception in order to remove the break keyword
}
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 2
Reputation: 4707
The answer to your question is yes, but the implementation of such a construct depends on your needs.
The direct (and inappropriate) way would be:
public void run() {
MyException ex = null;
while(ex == null) {
try {
// Do stuff
} catch(MyException e) {
// Maybe handle this exception
ex = e;
}
}
}
But this is a strange form of logic which could be simplified to:
public void run() {
while(true) {
try {
// Do stuff
} catch(MyException e) {
// Maybe handle this exception
break;
}
}
}
Or this, which is my preference of these three:
public void run() {
try {
while(true) {
// Do stuff
}
} catch(MyException e) {
// Maybe handle this exception
}
}
Despite all of these possibilities, since you have throws MyException
already in your run
signature, supposing your caller handles it properly you could just do this:
public void run() throws MyException {
while(true) { // Or maybe some exit condition?
try {
// Do stuff
}
}
}
This lets the exception propagate to the caller. Then, have the caller handle the resulting exception:
try {
myObject.run();
} catch(MyException e) {
// Handle this exception
}
The structure you'd want to use depends on your flow of logic. Consider which entity should handle your custom exception. What does it mean to have this exception thrown? Who/What would be responsible for handling such a case?
Upvotes: 3
Reputation: 7001
Yes, you could do it like this:
public class Example {
public static void run() throws IOException {
if (Math.random() > 0.75) {
throw new IOException();
}
}
public static void main(final String... args) {
boolean thrown = false;
while (!thrown) {
System.out.println("Roll");
try {
run();
} catch (IOException ex) {
thrown = true;
}
}
System.out.println("Done");
}
}
Which on an example roll would output:
Roll
Roll
Roll
Roll
Roll
Done
This is likely a bad idea to implement, as it will make the code harder to read, harder to maintain, and the overhead of throwing an exception will likely make the code slower too.
Upvotes: 0