• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementation Opinions Wanted...

 
Michael Vargenstien
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I'm implementing a one of the interfaces for my project with the following logic:



Opinions: do you think its a good practice to use a catch clause as a recoverable? I'd like to reuse what I've already done with the find method.

What do you think?
 
Lucy Hummel
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Hi Michael,

In general that is what I would implement, if I have data that has keys defined.
 
Abdel Kader
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, in general i would say that an exception is an "exception". Hence you should not use exceptions when the outcome is considered "normal". If a record does not already exist when creating a new one it is expected and therefore no exception should be thrown. In your case the method throws an exception either way which I would consider bad practice as there is no 'expected' behaviour.

Definitions from Sun:
"An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions."

"The Java programming language uses exceptions to handle errors and other exceptional events."

After all, the same people are going to grade that method of yours so I would recommend not to expect an exception.
 
Vincent Li
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

IMHO, the proper way to "reuse" your find code is extract the common non-exception code in a separate method, such as "exists(data)". Otherwise, since the find is suppose to return a list of all index that matches the criteria, it really isn't what you want:

 
Michael Vargenstien
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent Points Guys. I think an exists method makes sense and is probably the more proper way of doing it. That essentially would just use a contains method which would inform the calling object what is appropriate and what action should be taken.

 
Jonathan Cole
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definately do not use the exception handling framework for anything other than abnormal behavior. The exists() is a far better approach.
 
Ziji (Jay) Zhang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't checking record exists and calling dataManager.create(),
be in the synchronized block?
Other thread may change the data between these two calls.
Ziji
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic