Results 1 to 5 of 5
- 12-19-2011, 08:02 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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!
Java 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); }
- 12-19-2011, 08:15 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: try/catch and if statement conundrum
Consider not using an exception:if gameA is over 2 or gameB is over 0 it should say what's written in the exception
Your code would be more readable if your replaced A and B with a and b - or, better yet, meaningful variable names.Java Code:if (A == 2 && B == 0){ // etc } else if(A > 2 || B > 0) { // etc }
- 12-19-2011, 06:19 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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.
The exception class is thisJava 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)); }
and...Java Code:public class GameOver { public GameOver(int gameA, int gameB) throws GameOverException1 { if (gameA > 2 || gameB > 0) throw new 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.Java Code:public class GameOverException1 extends Exception{ public GameOverException1() { } }
- 12-19-2011, 06:35 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: try/catch and if statement conundrum
Cross posted at Having trouble over here, sorry if the answer is obvious.....
- 12-19-2011, 10:20 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: try/catch and if statement conundrum
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.I guess it won't make sense unless I put up a full code of it.
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.Last edited by pbrockway2; 12-19-2011 at 10:27 PM.
Similar Threads
-
try catch help
By vividcooper in forum New To JavaReplies: 8Last Post: 02-11-2010, 09:00 AM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
try catch!?
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 01-28-2008, 07:51 PM -
when to use try...catch
By javaplus in forum New To JavaReplies: 2Last Post: 11-18-2007, 08:52 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks