try/catch and if statement conundrum
I want the exception to actually run but it won't, the exception class is no worry it's fine, the exception is if gameA is over 2 or gameB is over 0 it should say what's written in the exception, its just it won't run when I reach the point in which the exception would activate. The only way it would activate is if I take off the brackets in the if statement but then the text overlaps with the beginning text of the program I'm making. The exception won't start!
Code:
try {
if (A == 2 && B == 0){
g.setColor(Color.magenta);
g.drawString("(random text", 1, 1);
g.drawString("random text", 1, 1);
g.drawString("It bit you and now your one,", 1, 1);
new GameOver(A,B);
}
}
catch (RandomException e){
g.drawString("No more, this program stops!", 1, 1);
}
Re: try/catch and if statement conundrum
Quote:
if gameA is over 2 or gameB is over 0 it should say what's written in the exception
Consider not using an exception:
Code:
if (A == 2 && B == 0){
// etc
} else if(A > 2 || B > 0) {
// etc
}
Your code would be more readable if your replaced A and B with a and b - or, better yet, meaningful variable names.
Re: try/catch and if statement conundrum
I'm kinda stuck to using exceptions and I guess it won't make sense unless I put up a full code of it.
Code:
if (IsOverButtonA && ClickedDown) {
gameA += 1;
}
if (IsOverButtonB && ClickedDown) {
gameB += 1;
}
//.....................// Bunch of coding
if (gameA == 2 && gameB == 0) {
g.setColor(Color.black);
g.drawString("You touched the body and it started", (x1 + 25), (y1 + 40));
g.drawString("to moan and it was a zombie!", (x1 + 25), (y1 + 60));
g.drawString("It bit you and now your one,", (x1 + 25), (y1 + 80));
g.drawString("great job", (x1 + 25), (y1 + 100));
g.setColor(Color.red);
g.drawString("THE END", (x1 + 100), (y1 + 120));
}
The exception class is this
Code:
public class GameOver {
public GameOver(int gameA, int gameB) throws GameOverException1 {
if (gameA > 2 || gameB > 0)
throw new GameOverException1();
}
}
and...
Code:
public class GameOverException1 extends Exception{
public GameOverException1() {
}
}
I want to use this exception within the if statement, because what I want to do this I want "Game Over" to be drawn in the applet but I also have many other situational gameA's and gameB's with if statements that have their own endings too and want to put exceptions on them as well.
Re: try/catch and if statement conundrum
Re: try/catch and if statement conundrum
Quote:
I guess it won't make sense unless I put up a full code of it.
I agree. If you decide to do that and find that runnable code that illustrates the problem is rather lengthy then consider constructing an SSCCE.
It may be that you're not as stuck using the exception as you think, then again you might be. We really can't tell until we know *what* you are trying to do, rather than how you are trying to do it.
[Edit, read ending more closely] From the sound of it you have a situation where you want to output "game over" (and possibly other things like write a score to a database, exit the program, etc...) that occurs in numerous games. It's not surprising that it might reoccur because a game coming to an end is quite commonplace. It is not ... exceptional. Which is what prompted my suggestion that you consider not using an Exception.