This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Exception not thrown  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, How come the exception error message in the setter method setPrice is never thrown if the price of the car is less than 50,000? the message never appears in the console when the program is run.








Thank you.

 
Marshal
Posts: 5999
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess because the method setPrice() is never called.  You can test that by putting a System.out.println() as the first line of the method.  But notice that when you create the Car object, you don't use setPrice().
 
Kevin O'Sullivan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it working,bye adding the set method to the constructor.






Exception in thread "main" java.lang.IllegalArgumentException: Error price too high!
at Car.setPrice(Car.java:42)
at Car.<init>(Car.java:14)
at CarType1.<init>(CarType1.java:7)
at Car_Tester.main(Car_Tester.java:7)



Is there anyway for to just print out the Error message "Error price too high !" instead of the rest ?
 
Marshal
Posts: 64496
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to alter the setPrice() method because you are calling a public method from the constructor. Methods called from the constructor should be private or final (no need to be both). By writing a setXXX() method, you are making it difficult to make the object immutable.
 
Knute Snortum
Marshal
Posts: 5999
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there anyway for to just print out the Error message "Error price too high !" instead of the rest ?


Well, you could just use System.out.println() and then call System.exit(1), but I think the exception is the way to go.  
 
Kevin O'Sullivan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You will have to alter the setPrice() method because you are calling a public method from the constructor. Methods called from the constructor should be private or final (no need to be both). By writing a setXXX() method, you are making it difficult to make the object immutable.



I'm not sure I understand could you give an example?
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That prevents nasty surprises if anybody tries to override that method in a subclass.
Please space your code out more: one empty line between successive methods, please. Please also consider whether you need all those setXXX() methods, but setPrice() is the one you would want to retain if you retain any setXXX() methods at all.
 
Kevin O'Sullivan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you catch it with a try/catch in your tester class? and if you can how would you do it?


 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:Could you catch it with a try/catch in your tester class? and if you can how would you do it?

At which point you realise that you should have used a more informative error message for your exception. As a general rule of thumb, you shouldn't catch unchecked exceptions, but this looks like a situation where that is a reasonable thing to do. There are much more elegant ways to create such Cars; the nextInt() method is overridden so as only to return values in a pre‑defined range, so you can avoid the exception altogether. I shall let you work out how the KeyboardInputs class works.
 
Kevin O'Sullivan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that!
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!