File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Assuming data from server is always correct. Catching unchecked exceptions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Assuming data from server is always correct. Catching unchecked exceptions?" Watch "Assuming data from server is always correct. Catching unchecked exceptions?" New topic
Author

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

Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 925
    
  14

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?


Formely Pawel Pawlowicz
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 925
    
  14

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
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

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.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 739
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

Joined: Apr 18, 2013
Posts: 925
    
  14

Yeah, I am still trying to overdo this...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

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

Joined: Apr 18, 2013
Posts: 925
    
  14

So I'll need to revert my Record class to store everything as strings again ;P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assuming data from server is always correct. Catching unchecked exceptions?