|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 3
Sometimes, catching Exception makes sense, such as in your application's last-ditch error handler or in the execution engine of an application server where
you don't want a coding error in one service to bring down the whole server. But most code doesn't need this special treatment.
Think twice!
Note that IBM's Java compiler, Jikes, does warn you about catching Throwable or Exception without explicitly throwing them—as well as many other items not caught by Sun's compiler. However, it doesn't warn about
catching an exception superclass when only a subclass is thrown. I don't have enough experience with Jikes to recommend using
it routinely, but it's easy to try, and I certainly recommend checking it out if you're interested.
To sum up, Java allows you to lose error-handling information when you catch an exception: you can catch a superclass instead
and lose the specific information conveyed by the subclass. If you catch Exception or Throwable, you won't even be told if your code doesn't throw an exception at all. So, how to stay out of trouble?
Exception or Throwable if you can help it.
Exception or Throwable, consider handling RuntimeException or Error separately from other Exception or Throwable subclasses. And follow the golden rule: never throw Exception or Throwable yourself.
With these guidelines in mind, you'll be better prepared to write even more exceptional code.
Read more about Tools & Methods in JavaWorld's Tools & Methods section.