Results 1 to 3 of 3
  1. #1
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default Confused??? Exception problem...

    I'm not understanding what I did wrong on this code but here is the code and then the build output.
    Java Code:
    public abstract class Employee throws MissingNameException, MissingSSNException
    {
      /*
       *	You will need to add a throws clause to the class header.
       *	List each of the exceptions that may be thrown by the constructor
       */
       private String firstName;
       private String lastName;
       private String socialSecurityNumber;
    
       // three-argument constructor
       public Employee( String first, String last, String ssn )
       {
       	/*
       	 *	Before executing each "set" method, test the argument.
       	 *		Example:  if first is a null string, throw the exception indicatign a missing name,
       	 *						else, invoke setFirstName method
       	 */
          setFirstName( first );
          setLastName( last );
          setSocialSecurityNumber( ssn );
    
       } // end three-argument Employee constructor
    
    
       // set first name
    
       	public void setFirstName( String first ) throws MissingNameException
       	{
          	if (first == null)
       		{
       			try
       			{
       				throw new MissingNameException();
       			}
       			catch(MissingNameException missingNameException)
       			{
       				System.out.println(missingNameException);
       			}
       		}
       		else
       		{
       			firstName = first; // should validate
       		} // end method setFirstName
       	}
    
       // return first name
       public String getFirstName()
       {
          return firstName;
       } // end method getFirstName
    
       		// set last name
       	public void setLastName( String last ) throws MissingNameException
       	{
       		if (last == null)
       		{
    			try
       			{
       				throw new MissingNameException();
       			}
       			catch(MissingNameException missingNameException)
       			{
       				System.out.println(missingNameException);
       			}
       		}
       		else
       		{
       			lastName = last; // should validate
       		} // end method setLastName
       	 }
    
       // return last name
       public String getLastName()
       {
          return lastName;
       } // end method getLastName
    
       	// set social security number
       	public void setSocialSecurityNumber( String ssn ) throws MissingSSNException
       	{
       		if (ssn == null)
       		{
       			try
       			{
       				throw new MissingSSNException();
       			}
       			catch (MissingSSNException missingSSNException)
       			{
       				System.out.println(missingSSNException);
       			}
       		}
       		else
       		{
          		socialSecurityNumber = ssn; // should validate
       		} // end method setSocialSecurityNumber
       	}
    
       // return social security number
       public String getSocialSecurityNumber()
       {
          return socialSecurityNumber;
       } // end method getSocialSecurityNumber
    
    
       // return String representation of Employee object
       @Override
       public String toString()
       {
          return String.format( "%s %s\nsocial security number: %s",
             getFirstName(), getLastName(), getSocialSecurityNumber() );
       } // end method toString
    
    
    }// end abstract class Employee
    BUILD OUTPUT

    Java Code:
     '{' expected
    public abstract class Employee throws MissingNameException, MissingSSNException
                                  ^
    1 error
    
    Process completed
    Here is the task view.....
    Java Code:
    '{' expected
    Please help!

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

    Default

    What have you tried to fix it? With a glance it looks like you have messed up brackets somewhere.

    Also, in some of your methods you throw an exception and immediately catch it, what's the point? You should just throw the exception when necessary, then when you call the method you handle possible exceptions.

    This could be wrong, but it's easy to check, try removing the throws from the class and only have the methods contain the throws clause.
    Last edited by sunde887; 07-06-2011 at 08:52 AM.

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

    Default

    Methods can throw Exceptions, classes can't and the 'throws' keyword in a class definition doesn't make sense so the compiler wants a left curly bracket where it saw the 'throws' keyword.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Confused in Exception Handling Assignment!!
    By rugger06 in forum New To Java
    Replies: 5
    Last Post: 04-10-2011, 02:00 PM
  2. Exception problem !!
    By chathura87 in forum New To Java
    Replies: 6
    Last Post: 02-21-2011, 04:02 PM
  3. Problem Configuring the JDK (slightly confused)
    By lumzi23 in forum New To Java
    Replies: 2
    Last Post: 12-23-2010, 06:12 PM
  4. Exception handling problem
    By computerbum in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 03:15 PM
  5. Problem using EBI Webservice getting Exception
    By tealc76 in forum Advanced Java
    Replies: 0
    Last Post: 11-21-2009, 06:20 PM

Posting Permissions

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