views:

139

answers:

5

As Error and Exception are subclass of throwable class, we can throw any error, runtime ex and other ex. Also we can catch any of these type.

Why do we usually catch only checked Exception?

Can somebody provide me good links for exception with examples?

+3  A: 

I think your premise that unchecked exceptions are never handled is incorrect. Sure, you should never handle NullPointerException, IndexOutOfBoundsException, etc. since these indicate logic errors in the programming, and so catching them actually hides the fact that there is a bug in the program. However, some APIs have exception hierarchies that are rooted in RuntimeException and, if that is the case, then you may end up catching a whole hell of a lot of those in your application; in fact, I previously developed a GUI application, where a huge number of different exceptions that inherited from RuntimeException needed to be handled, and this API happened to be used more than any of the APIs in the Java language that throw checked exceptions, and so it was actually the opposite in that case.

That said, if there is something that absolutely does need to be handled, by convention, these are made into checked exceptions (inheriting from Exception instead of RuntimeException). Also, the compiler forces you to handle such an exception or declare it to be thrown (that's what "checked exception" means). So, a combination of convention and requirement is probably why you may be handling more checked than unchecked exceptions (because the ones that are urgent have been made into checked exceptions by convention, or you are ignoring important unchecked exceptions, because you're not forced to handle them).

Michael Aaron Safyan
Unless I'm missing something, that sounds like a really awful design. If these exceptions were expected to be handled, why would you have them inherit from `RuntimeException`?
danben
It might be a case of exceptions that you'd like to handle 'sometimes'. E.g. Spring's JDBC library re-throws SQLExceptions as unchecked exceptions, since you might want to handle them at the application layer, or let them propagate to the container.
harto
@danben, they were unchecked because intermediate layers don't need to know about them, but they can happen, and letting the user see an ugly print out is not a good idea... typically they were for operations that sometimes fail and simply need to be restarted in those cases.
Michael Aaron Safyan
What's the standard practise for showing a user friendly message when an Exception is thrown?
James P.
@James, I don't know about other people, but for myself.... if it is due to invalid input, then I use a label next to the input field with red text indicating the input requirement (much like is done in JavaScript on most web pages with forms). If it is unrelated to user input, and it is something that can be fixed by trying again (e.g. the network temporarily went down, and so we need to reconnect), then I typically retry a fixed number of times, before alerting the user that they aren't connected to the Internet (and schedule a later retry attempt, so that it works once they reconnect)...
Michael Aaron Safyan
... If the problem is something that nothing can be done about, the very last thing I want to do is show the user an annoying message box... unless it is a severe problem that will cause the application to quit, I typically just log the error, since nothing can be done.
Michael Aaron Safyan
+6  A: 

The whole point of a checked exception is that you are required to catch it. It is the type of error that can potentially come up in normal program execution (such as an I/O error), and the application is expected to handle it gracefully.

Conversely, unchecked exceptions are those that should not occur during normal operation of the program (they break the rules in some way), and as such you do not try to handle it since there isn't likely much you can do.

danben
+1  A: 

Why do we usually catch only checked Exception?

Because unlike unchecked exceptions (RuntimeException, Error, and their subclasses), you are required to catch them. Inversely, the point of using unchecked exceptions is that you're not required to catch them and the common scenario it to let a piece of software "above" your application (typically a framework, a container, or the JVM) handle them. In some cases, you might need or decide to handle one yourself though and catch it explicitly. But that's a kind of exceptional situation.

Can somebody provide me good links for exception with examples?

Maybe the Java Tutorials about Exceptions.

Pascal Thivent
Exceptional answer
GregS
A: 

Note: checked exceptions are somewhat controversial, and have fallen out of favor in many circles (e.g. Spring Framework wraps many checked Exceptions from Java Libraries in RuntimeException Hierarchies, often combined with the "fault barrier" error handling pattern.)

jayshao
A: 

Why do we usually catch only checked Exception?

Checked exceptions provide a way of forcing the programmer to think about handling predictable errors in a nice way. These "predictable" errors often occur when dealing with I/O and networking. We much less frequently catch RuntimeExceptions as they are less predictable than checked exceptions and catch cover up mistakes in the programming logic. For example, catching NullPointerException is a lazy way of dealing with null values, when the better solution would be to figure out why an object is null and deal with it directly. Of course, there will be times when you will want to catch RuntimeExceptions like NumberFormatException.

Note that checked exceptions are more important in Java than most languages, and even languages built on the JVM like Scala do not have checked exceptions.

Can somebody provide me good links for exception with examples?

See http://www.javaworld.com/javaworld/jw-07-1998/jw-07-exceptions.html for a good article on Java exceptions.

Justin Ardini