Results 1 to 6 of 6
  1. #1
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Casting a Throw Exception

    Why won't my throw exception work?!?!

    DRIVER:
    Java Code:
    public class Driver {	public static void main(String[] args)
    	{
    		//MAIN IS FOR TESTING PURPOSES ONLY
    		//object created with new class; example of setting age&name + output results
    		Person me=new Person();
    		me.setAge(125);
    		me.setName("Robert");
    		
    		System.out.println(me.getName() + " is currently " + me.getAge() + " years old.");
    		
    		//PRINTING THE OBJECT ITSELF
    		System.out.print(me);
    
    
    	}
    
    }
    CLASS:
    Java Code:
    public class Person {
    
    	//variables that store a Person's weight and age(not directly accessable by client)
    	private int age;
    	private String name;
    	
    	//mutator method that makes sure the person is of valid age. Error otherwise.
    	public void setAge(int a) throws IllegalArgumentException{
    		if(a>=1||age<=95) 
    		{
    			//changes the instance variable based on clients input.
    			age=a;
    		}
    		else
    		{
    			throw new IllegalArgumentException("Invalid Age.");
    		}
    	}
    	
    	//mutator method that assigns what the client inputs to the instance variable
    	public void setName(String n)
    	{
    		name=n;
    	}
    	
    	//accessor method that returns the values in the corresponding instance variable (the person's age).
    	public int getAge()
    	{
    		return age;
    	}
    	
    	//accessor method that returns the values in the corresponding instance variable (the person's name).
    	public String getName()
    	{
    		return name;
    	}
    }
    OUTPUT: Robert is 125 years old.

    Shouldn't it display an error? (DUE TO MY THROW EXCEPTION )

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Casting a Throw Exception

    if(a>=1||age<=95)

    1. why age ? do you mean "a" too here or? :)
    2. because the first condition is true 125 >= 1 --> yes, so the second condition is not tested
    3. try && instead of || :D

    --> if (a >= 1 && a <= 95) {

  3. #3
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Casting a Throw Exception

    THANK YOU YOUR A GENIUS :D - hits myself -

    Last quesiton: when printing the object itself, why does it output: Person@4e82701e

  4. #4
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Casting a Throw Exception

    When you print an instance of a class you made yourself, it uses the default implementation of toString(), unless you explicity override it, like so:

    Java Code:
    class Person{
        //constructor(s), field(s), other method(s)
    
        @Override
        public String toString(){
            //return whatever you want to be printed when you print the object itself
        }
    }

  5. #5
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Casting a Throw Exception

    so why did it print out 'Person@4e82701e'

    What does toString() do?? where did everything after the @ come from?

  6. #6
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Casting a Throw Exception

    The thing after the @ is the hexadecimal memory address of the object.

Similar Threads

  1. Replies: 1
    Last Post: 02-06-2011, 05:38 AM
  2. Throw Exception in this code
    By eng_hyzoom in forum New To Java
    Replies: 9
    Last Post: 12-22-2010, 06:18 PM
  3. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 03:45 PM
  4. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 04:35 AM
  5. throw an exception
    By sfe23 in forum New To Java
    Replies: 3
    Last Post: 02-14-2009, 04:41 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
  •