-2

can someone tell me why am i getting "cannot find symbol" while compiling.

This is how i define my own exception:

public void setIdNumber(int i)throws InvalidEmployeeId
  {
  idNumber = i;

   if ((idNumber <= 0))
     {
        throw new InvalidEmployeeId("invalid ID. Please input numeric ID");
     }

  }

cannot find symbol:

public void setIdNumber(int i)throws InvalidEmployeeId

symbol: class InvalidEmployeeId

Here is the try and catch statement in the demo program:

 try 
   {
     id = Integer.parseInt(input);
     worker.setIdNumber(id);
   } catch (invalidEmployeeId e)
   {
     System.out.println(e.getMessage());
   }

thanks in advance;

Aduait Pokhriyal
  • 1,529
  • 14
  • 30
user3080461
  • 33
  • 3
  • 8

1 Answers1

1

Add this class to your code:

public class InvalidEmployeeId extends Exception
{

public InvalidEmployeeId(String string)
{
    super(string);
}

}

Just saying new does not define the Exception for you. Furthermore, you must be consistent in your usage of the class names.

try 
 {
 id = Integer.parseInt(input);
 worker.setIdNumber(id);
 } catch (InvalidEmployeeId e) //must be a capital I!
 {
 System.out.println(e.getMessage());
 }

If you already defined an ivalidEmployeeId Exception subclass, then you must change this line:

 throw new invalidEmployeeId("invalid ID. Please input numeric ID"); 
Azar
  • 1,086
  • 12
  • 27
  • I think he already have it, his problem is `catch(invalidEmploteeId)`. – Maroun Apr 06 '14 at 13:47
  • @MarounMaroun Yeah, one of those names is definitely wrong... – Azar Apr 06 '14 at 13:49
  • i changed i to I, however, still getting the same error – user3080461 Apr 06 '14 at 13:57
  • @user3080461 Let's take it from the top. Have you defined, _anywhere_, a new class for your exception, as I posted in my answer. – Azar Apr 06 '14 at 14:00
  • Azar: i did not define InvalidEmployeeId anywhere in the sub class. I have no idea why am i getting this error. – user3080461 Apr 06 '14 at 14:01
  • can u explain more about putting super in the constructor please? – user3080461 Apr 06 '14 at 14:02
  • @user3080461 You are getting this error because **you must define** a new class for your exception! In order to `throw` `InvalidEmployeeId`, you have to create it first! – Azar Apr 06 '14 at 14:03
  • @user3080461 Because it extends `Exception`, I am calling the `Exception` constructor that takes a string. See [this](http://docs.oracle.com/javase/tutorial/java/IandI/super.html). – Azar Apr 06 '14 at 14:04
  • i still can't figure out how to fix this. any thoughts please. – user3080461 Apr 06 '14 at 14:14
  • does the word after extends take the name of the demo program or it should always be Exception? – user3080461 Apr 06 '14 at 14:16
  • @user3080461 Everywhere in your code, use `InvalidEmployeeId`, with a capital I. Then, copy and paste my code for that class (`InvalidEmployeeId`) into a new class of the same name in your IDE. And yes, it should be `Exception`, as I have posted, and nothing else (for your purposes). – Azar Apr 06 '14 at 14:18