• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Throws clause throws compile error

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ray Marsh:


What am I missing?

Thanks



A class named "InvalidStateException".
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, where in your list of states is DC, Puerto Rico, Guam and other postal abbreviations?
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thank you.
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic