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: catch negative numbers

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default catch negative numbers

    Hello,
    I do not understand what is wrong in my code. I am trying to catch negative values, but the program is just running as normal, accepting them.
    package perfectSquareException2;
     
    import java.lang.*;
    import java.util.Scanner;
     
    public class perfSqEx {
     
     
     
     
    			public static void main(String args[] ) {
     
    			Scanner in= new Scanner(System.in);
    			double number = 0;
    			double a = 0;
    			boolean square = true;
    			boolean correct = false;
     
     
    			System.out.println("Enter positive number: ");
    			try {
    			number = in.nextDouble();
    			if (number > 0)
    				correct = true;
     
    			}catch (Exception exception) {
    				if (number < 0)
    				System.err.println(exception.getMessage() + "\n");
     
    		 }
     
    			if((Math.sqrt(number) * 10) % 10 == 0.0 ) {
     
    				square = true;
    					System.out.println("yes");
     
    				}else
    				{
    					square = false;
    					System.out.println("no");
    				}
     
    }
    }


    --- Update ---

    I know that I can create my own exception that will inherit general Exceptions class. Bt in this task I have to use exactly try catch block with the exception of type Exception.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: catch negative numbers

    There currently is no reason for your logic to enter the catch block, because there's nothing in the try{} block that can throw an exception. You might review this tutorial for pointers.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: catch negative numbers

    Thanks for reply, Greg.
    However, I am sure it can be done. I might have wrong coding for that or even wrong order. What i am trying to say in this program:
    the users tries to enter numbers and once negative number is entered, the system should catch the number and throw the exception.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: catch negative numbers

    Did you look at the tutorial I sent you?

    Of course it can be done, and I know what you're trying to say. You said it well the first time, but I'm not certain you hear so well. "The System" doesn't just throw unchecked exceptions by itself and this isn't even an unchecked exception. You just want to throw one, and that's fine, but where do you throw the exception? Read the tutorial and learn how to throw an exception. Something like:
    try
    {
        if ( this bad thing happens )
        {
            throw an exception
        }
    }
    catch
    {
        deal with the exception
    }

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: catch negative numbers

    Quote Originally Posted by GregBrannon View Post
    There currently is no reason for your logic to enter the catch block, because there's nothing in the try{} block that can throw an exception.
    Yes there is, the call to nextDouble. The issue with the code is that the if statement handling the negative number is in the catch statement but a negative number will not cause an exception.
    Improving the world one idiot at a time!

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: catch negative numbers

    I must be wearing a "Junky Kick Me" sign tonight.

    You're absolutely right. I was wrong. I believe I was thinking, " . . . there's nothing in the try{} block that can throw an exception . . . " if the user enters a negative number. I apologize if I misled the OP or caused anyone confusion by being imprecise in my response. I'll be waiting in the woodshed.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: catch negative numbers

    Quote Originally Posted by GregBrannon View Post
    I'll be waiting in the woodshed.
    Chop some wood while you're in there.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 3
    Last Post: November 18th, 2013, 11:55 AM
  2. Replies: 4
    Last Post: February 10th, 2013, 11:58 PM
  3. Have trouble dealing with negative numbers.
    By Arick Cheng in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 29th, 2012, 07:41 AM
  4. Method returning negative numbers seemingly at random
    By NcAdams in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2012, 09:53 AM
  5. Negative
    By xew123 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 07:25 PM