• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Exception not thrown

 
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.

 
Sheriff
Posts: 6193
164
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 65474
249
  • 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
Sheriff
Posts: 6193
164
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 65474
249
  • 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: 65474
249
  • 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!
 
A wop bop a lu bop a womp bam boom! Tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!