Having trouble over here, sorry if the answer is obvious.....
(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. I've also tried putting the try statement outside of the if but that's useless too....)
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);
try {
new GameOver(A,B);
}
catch (RandomException e){
g.drawString("No more, this program stops!", 1, 1);
}
}
Re: Having trouble over here, sorry if the answer is obvious.....
Isn't it reaching to the statement inside try ?
Re: Having trouble over here, sorry if the answer is obvious.....
Here I changed it up, I want the exception to work but it seems with the if statement and the booleans within it make it conditional in which I thought making a "new" exception class would make it work but somehow it never seems to pick it up
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: Having trouble over here, sorry if the answer is obvious.....
What exception you want to throw? What is GameOver(A,B). When do you want to throw exception?
Re: Having trouble over here, sorry if the answer is obvious.....
a) Please do not post the same question twice to the forums - I have deleted your other post b) Please disclose cross posts
This thread has been cross posted here:
http://www.java-forums.org/new-java/52887-try-catch-if-statement-conundrum.html
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting
Re: Having trouble over here, sorry if the answer is obvious.....
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: Having trouble over here, sorry if the answer is obvious.....
Where is the class RandomException defined?
How is it related to the GameOverException1 class?
Try debugging your code by adding printlns to show the execution flow.
Then copy and paste here the print out and the program that goes with it.