Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Question 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);
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: try/catch and if statement conundrum

    if gameA is over 2 or gameB is over 0 it should say what's written in the exception
    Consider not using an exception:

    Java 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.

  3. #3
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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.

    Java 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

    Java Code:
    public class GameOver {
    
    	public GameOver(int gameA, int gameB) throws GameOverException1 {
    	      if (gameA > 2 || gameB > 0)
    	         throw new GameOverException1();
    	}
    }
    and...

    Java 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.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: try/catch and if statement conundrum


  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: try/catch and if statement conundrum

    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.
    Last edited by pbrockway2; 12-19-2011 at 10:27 PM.

Similar Threads

  1. try catch help
    By vividcooper in forum New To Java
    Replies: 8
    Last Post: 02-11-2010, 09:00 AM
  2. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  3. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •