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 5 of 5

Thread: Need Help Writing Exception code.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help Writing Exception code.

    Hey y'all, this is probably a simple a question but i'm not used to writing exceptions...
    The program is easy, basically I had to make a program that takes the user input on a length of a square, then it prints out the square in *''s, and then states the area and perimeter, i have all that, but then you have to write an exception class if the user inputs a number such as "0" which is invalid.

    So here is my driver....
    import java.util.*;
    public class SquareDriver {
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		String input ="";
    		System.out.println("Welcome to the easy square program");
    		while(true)
    		{
    			System.out.println("Enter the length of the side of a square or enter QUIT to quit");
    			try
    			{
    				input = keyboard.nextLine();
    				if(input.equalsIgnoreCase("quit"))
    					break;
    				int length = Integer.parseInt(input);
    				Square s = new Square();
    				s.setLength(length);
    				s.draw();
    				System.out.println("The area is "+s.getArea());
    				System.out.println("The perimeter is "+s.getPerimeter());
    			}
    			catch(DimensionException e)
    			{
    				System.out.println(e.getMessage());
    			}
    			catch(Exception e)
    			{
    				System.out.println(e.getMessage());
    			}
    		}
    	}
    }

    and then here is my Dimension Exception, I don't even want an entire answer, if someone could kindof help point me into the direction to get started it would be much appreciated!

    public class DimensionException extends Exception
    {
     
    }
    Last edited by USCPaddler; November 10th, 2011 at 01:57 AM. Reason: Use highlight tags!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need Help Writing Exception code.

    I think you're doing step 2 (catching the Exception), when you want to be doing step 1 (throwing the Exception). That way whoever calls your code can catch the Exception you throw. You don't do the catching, they do. You do the throwing.

    So the question is, when do you want to throw your Exception?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Need Help Writing Exception code.

    The driver was all given by my professor, and told not to alter the driver code.
    If the dimension is set improperly throw the DimensionException

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

    Default Re: Need Help Writing Exception code.

    What about the Square class? Is the setLength method supposed to throw a DimensionException?
    Improving the world one idiot at a time!

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need Help Writing Exception code.

    The question remains: when do you want to throw your Exception?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What is Wrong With My Exception Class Code?
    By Allicat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2011, 10:09 AM
  2. writing java code help
    By jaisan72980 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 12th, 2011, 03:39 PM
  3. Replies: 1
    Last Post: May 12th, 2010, 08:54 AM
  4. Trouble writing some code...help?
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 7th, 2010, 08:54 PM
  5. Help writing some Scanner code
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 3rd, 2010, 04:27 AM