Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Swallowing Exceptions - gulp!

 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen it stated in a couple of threads in the SCJD Forum on this site (and also, I think, in Max Habibi's SCJD book) that swallowing exceptions is always a and bad developers will be condemned to eternal damnation (or some other spotty dog ).

My question is, what is wrong with code like the example below, when you're releasing some resource back to the environment, possibly just before shutdown, and you really don't care if it makes a little pop?




The only argument I can see in favour of doing something is just to report (log, or whatever) the exception for the hell of it...

Any views?

Jules
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing is wrong with the code... well it will compile and run and no ressources will be lock after execution. The first letter of your method must be lowercase... that's the main error in your code...
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input Alain.

Perhaps I should clarify. I'm looking for feedback on the practice of swallowing exceptions. The code compiles and executes fine as it is.

The code snippet is the constructor of the SwallowEg class.

Jules
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every rule is made to be broken. What's definitely wrong is the newbie practice of telling the compiler to "shut up, already!" by adding empty catch blocks.

But for the experienced programmer, there are plenty of cases where an empty catch block may be a reasonable choice. Surrounding close() is one example; if there's an error closing a file, there's probably nothing you can do about it. If it's the user's important data, though, you still might want to report it. The important thing here is that it's a conscious decision on the developer's part, not just a reflex.

There are other cases where you know that a method will throw only under certain circumstances, and you know that those won't hold at a particular invocation. Again, though, it often makes sense to log those to a debug log, because if they do happen anyway, it indicates a faulty assumption (i.e., a logic bug.)

When I do this, I often use a variable name like "silentlyIgnore" for the exception object in the catch clause to make it perfectly clear that I know what I'm doing.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree in principle: but in practice, I think you pretty much always want to at least log the Exception. For smaller systems, of course, it's less critical. But larger, more complicated systems really need to track their errors: things just get too messy if you don't.

As far the SCJD is concerned: I'm pretty sure the graders will knock you a bit if you leave empty catch blocks.

All best,
M
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic