• 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

Assuming data from server is always correct. Catching unchecked exceptions?

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Record class which is used in business layer. It holds a single record from a database.
It has several setter methods like setCustomerId(String id). Those methods may throw IllegalArgumentException.
May I assume that I will always get proper values from DB class?
I could catch IllegalArgumentException but that would mean catching unchecked exceptions and this should be avoided.
I could check the argument before passing it to Record but that would mean doubling the same code (checking inside and outside Record class).
What approach do you prefer?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 1st question to ask yourself: why do you have these checks inside the Record class?

If you have these checks to force another developer using the API correctly, it's of course not needed to catch these exceptions. Because these situations should never occur in a well-tested application and if they do occur, it's a developer mistake not a user one
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An example.
My setOccupancy(int) method throws IllegalArgumentException if a negative value is passed to it.
What if there is negative value in a .db file? The file will be fetcher to String[] and "-1" will be stored on index 5 (for example, I don't remember exact number).
Then when there is a need to read this record, business side will fetch "-1" string and parse it into -1 integer.
That integer will be passed to the method and an Exception will be thrown. What to do? Check parameters twice? Catch the exception? Something else?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My instructions have no requirements about occupancy, only something about the customer id. So why bother of implementing rules you have made up yourself and even might violate business rules you are not aware of And if you handle such situation, what about a non-numeric occupancy field in the database file?

I handled all data from the database file as-is, so I didn't add extra validations. On the client & server side I only validated the customer id, making sure it's an 8-digit number before saving to file.
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:I have a Record class which is used in business layer. It holds a single record from a database.
It has several setter methods like setCustomerId(String id). Those methods may throw IllegalArgumentException.
May I assume that I will always get proper values from DB class?
I could catch IllegalArgumentException but that would mean catching unchecked exceptions and this should be avoided.
I could check the argument before passing it to Record but that would mean doubling the same code (checking inside and outside Record class).
What approach do you prefer?



I always assume the data provided in the db file are valid.
It is up to you to check if the data is valid when you are reading the db file. If you read a negative integer for a room occupancy number , for example, then throw an exception and move on to read the next record. When the exception is thrown, don't create a Record instance.

Again, for simplicity, you don't need to worry about invalid data in the file.

If the room you are trying to reserve has been occupied, then show a warning box to let the user know the room cannot be reserved.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I am still trying to overdo this...
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:Yeah, I am still trying to overdo this...


Don't know if it's a consolation, but you're definitely not the only one People have serious difficulties to simply stick to the instructions and implement only what's needed
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'll need to revert my Record class to store everything as strings again ;P
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic