• 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

Implementation Opinions Wanted...

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Hi Michael,

In general that is what I would implement, if I have data that has keys defined.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic