• 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

Exception handling

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I just want to confirm if I've done this the correct way.
I have a code to fetch a record from database, given the ID of the record.



The problem is, apparently the clearParameters() call in the finally block throws an SQLException.
I managed to solve this by putting another try catch clause inside the finally block.



Problem solved, by I hate this. Exception handling is cluttering up my code !
Can someone give an advice, how should I solve this problem? Or this is how it is, and I should just leave it be?

 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ andree
Suppose if we handled SQLException in clearParameters() method is that good idea to handle it there?
Let see what other will say about it.
 
andree surya
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ninad Kulkarni wrote:@ andree
Suppose if we handled SQLException in clearParameters() method is that good idea to handle it there?
Let see what other will say about it.



Thanks for the response Ninad.
I forgot to mention that getByIdStatement is a PreparedStatement object, included in the Java standard library.
I need to call clearParameters() method to the the object because it will be reused later.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes excessive-looking exception handling is just something you have to do in Java. In this example, it's possible to avoid the repeated catch SQLException simply by putting a single catch SQLException after everything else, which simplifies a bit. However I would also add another try/finally to close the ResultSet, just because it's good practice to close anything with a close() method, in a finally block, as soon as you're done with it:

Note that I also didn't bother to print the SQLException, but I did include that exception in the DataSourceException constructor, which I think is more important. Whatever code catches that exception, it can print the full stack trace, and that will include the stack trace of the initial SQLException. That's useful information that we don't want to lose, and it's usually easier if, when the exception is eventually logged, all the stack trace info appears in one place.
 
andree surya
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Thanks for your code and advice, Mike !

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic