Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throws clause throws compile error

 
Bacon
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is part of a sample program I'm trying to compile.

public void setSTATE (String state) throws InvalidStateException {
String states[] = {"AL","AL","AZ","AR","CA","CO","CT",
"CE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA",
"ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH",
"NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC",
"SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY"};
if (state.length() != 2)
throw new InvalidStateException(state);
for (int i=0; i<states.length;i++) {
if (state.equals(states[i])) {
STATE = state;
return;
}
}
throw new InvalidStateException(state);
}

Upon compile I get this error.

Customer.java:193: cannot resolve symbol
symbol : class InvalidStateException
location: class Customer.Customer
public void setSTATE (String state) throws InvalidStateException {

The "catch" is defined in the main function:

public static void main (String[] args) {
Customer cust = new Customer(new BigDecimal("2"));
cust.print();
try {
cust.setSTATE("ZL");
} catch (InvalidStateException err) {}
try {
cust.setSTATE("MA");
} catch (InvalidStateException err) {}

What am I missing?

Thanks
 
Joe Ess
Bartender
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ray Marsh:


What am I missing?

Thanks


A class named "InvalidStateException".
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, where in your list of states is DC, Puerto Rico, Guam and other postal abbreviations?
 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you forget to import InvalidStateException Class in your source code....

kalai Selvan T.
 
Bacon
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I wasn't sure how the InvalidStateException was supposed to be defined.

The InvalidStateException class must be else where in the code. I'll try to find it.

This is not my code, it is a sample from a book.
 
Bacon
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic