Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Having trouble over here, sorry if the answer is obvious.....

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Having trouble over here, sorry if the answer is obvious.....

    Isn't it reaching to the statement inside try ?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

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

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

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

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default 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


  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
        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
        public class GameOver {
     
            public GameOver(int gameA, int gameB) throws GameOverException1 {
                  if (gameA > 2 || gameB > 0)
                     throw new GameOverException1();
            }
        }

    and...

     
        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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,336
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

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

Similar Threads

  1. Pls Answer...
    By sachin.garde in forum Exceptions
    Replies: 4
    Last Post: October 31st, 2011, 03:38 AM
  2. What's ur answer?
    By Tanmaysinha in forum The Cafe
    Replies: 6
    Last Post: October 12th, 2011, 07:26 AM
  3. [SOLVED] Answer always false.
    By tyb97 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 7th, 2011, 11:56 AM
  4. need answer for these
    By satishbs in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2011, 02:46 AM
  5. Does anyone have answer of this Assignment??
    By lulzimfazlija in forum Java Theory & Questions
    Replies: 3
    Last Post: January 21st, 2011, 05:02 AM