Results 1 to 17 of 17
  1. #1
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Exception Handling

    I'm trying to write a program that throws an exception if the input value is between 0 and 125 using exception errors. I'm not sure where to define the parameters.

    import java.util.Scanner;
    class InvalidAgeException extends Exception
    {
    public InvalidAgeException()
    {
    super("The Age you've entered is not valid");
    }
    }
    class InputAge
    {
    public static void main(String args[])
    {
    try
    {
    Scanner inputdata = new Scanner( System.in );
    System.out.print("Enter your name");
    inputdata.nextInt();
    System.out.print("Now enter your age");
    int x = inputdata.nextInt();
    }
    catch(InvalidAgeException ae)
    {
    System.out.println("Invalid Age");
    }
    finally
    {
    System.out.println("Program Execution Completed");
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Where in your code are you going to throw an InvalidAgeException? I don't see it anywhere.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Why create an exception? Just have an if statement...

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, of you post, please wrap our code in code tags.

    [code]
    YOUR CODE HERE
    [/code]

  6. #6
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    So now I've got the code in but it says "Exception in thread "main" java.lang.NoSuchMethodError: main"

    Java Code:
    import java.util.Scanner;
    class InvalidAgeException extends Exception
    {
    	public InvalidAgeException()
    	{
    		super("The Age you've entered is not valid");
    	}
    }
    class InputAge
    {
    	public static void main(String args[])
    	{
    		try
    		{
    			Scanner inputdata = new Scanner( System.in );
    			System.out.print("Enter your name");
    			inputdata.nextInt();
    			System.out.print("Now enter your age");
    			int x = inputdata.nextInt();
    			if (x < 0 && x > 125)
    			{
    				throw new InvalidAgeException();
    			}
    		}
    		catch(InvalidAgeException ae)
    		{
    			System.out.println("Invalid Age");
    		}
    		finally
    		{
    			System.out.println("Program Execution Completed");
    		}
    	}
    }

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Is your file called InputAge.java? Also, you need to make the InputAge class public.

  8. #8
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    now I got the program itself to work, but when i put in an age that is above 125, It doesn't throw an exception.
    Java Code:
    import java.util.Scanner;
    public class InvalidAgeException extends Exception
    {
    	public InvalidAgeException()
    	{
    		super("The Age you've entered is not valid");
    	}
    }
    class InputAge
    {
    	public static void main(String args[])
    	{
    		try
    		{
    			Scanner inputdata = new Scanner( System.in );
    			System.out.print("Enter your name");
    			String str1 = inputdata.nextLine();
    			System.out.print("Now enter your age");
    			int x = inputdata.nextInt();
    			if (x < 0 && x > 125)
    			{
    				throw new InvalidAgeException();
    			}
    		}
    		catch(InvalidAgeException ae)
    		{
    			System.out.println("Invalid Age");
    		}
    		finally
    		{
    			System.out.println("Program Execution Completed");
    		}
    	}
    }

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    You're checking to see if the age is below 0 AND above 125. That'd be rather tricky.

  10. #10
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    so do I have to create two separate exceptions?

  11. #11
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    While that would work, I think it would be easier to check if the age is below 0 OR above 125.

  12. #12
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    O...Duh! I can be pretty retarded sometimes...

  13. #13
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    nvrmnd. It still doesn't work
    Java Code:
    import java.util.Scanner;
    public class InvalidAgeException extends Exception
    {
    	public InvalidAgeException()
    	{
    		super("The Age you've entered is not valid");
    	}
    }
    class InputAge
    {
    	public static void main(String args[])
    	{
    		try
    		{
    			Scanner inputdata = new Scanner( System.in );
    			System.out.print("Enter your name");
    			String str1 = inputdata.nextLine();
    			System.out.print("Now enter your age");
    			int age = inputdata.nextInt();
    			if (age < 0 || age > 125)
    			{
    				throw InvalidAgeException(age);
    			}
    
    		}
    		catch(InvalidAgeException ae)
    		{
    			System.out.println("Invalid Age");
    		}
    		finally
    		{
    			System.out.println("Program Execution Completed");
    		}
    	}
    }

  14. #14
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    What, more precisely, doesn't work? It doesn't throw an exception when you enter 125 as the age?

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by link6790 View Post
    nvrmnd. It still doesn't work
    Java Code:
    public class InvalidAgeException extends Exception
    {
    	public InvalidAgeException()
    	{
    		super("The Age you've entered is not valid");
    	}
    [ ... ]
    				throw InvalidAgeException(age);
    [ ... ]
    You don't have a constructor with a parameter.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    darn, I replied twice; my keyboard mustn't be properly debounced ...
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Not sure exactly what I did to change it but now it works :). Thanks for all your help guys!

Similar Threads

  1. Exception Handling not working
    By collwill in forum New To Java
    Replies: 5
    Last Post: 05-02-2011, 05:37 AM
  2. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:00 AM
  3. Exception Handling
    By liljester in forum New To Java
    Replies: 4
    Last Post: 06-21-2010, 03:09 PM
  4. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM

Posting Permissions

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