Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Checked/Unchecked Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checked/Unchecked Exceptions" Watch "Checked/Unchecked Exceptions" New topic
Author

Checked/Unchecked Exceptions

Roberto Diaz
Ranch Hand

Joined: Mar 08, 2002
Posts: 40
I need some help in uncluttering my mind
about exceptions.
Could some one explain in plain english
what is the difference between checked and
a unchecked exception.
I have gone thru few examples and tech. documentation and cluttered my view.
TIA
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Rahul K:
Could some one explain in plain english
what is the difference between checked and
a unchecked exception.

Quick answer: an unchecked exception is any exception that extends RuntimeException. Errors could also fall into the realm of unchecked exceptions, although they're slightly different than a normal exception (at least semantically).
Long Answer: A Checked exception is an exception that the compiler will check for. The compiler ensures that you have provided a handler for that exception (even if it's empty) or it issues an error when you try to compile your code. For example, if you invoke the method Thread.sleep(), you must either catch the InterruptedException that it can throw (as this is a checked exception) or decalre that this method throws it. In such a case, the invoking method would be required to deal with the exception in the same way.
An unchecked exception is something that the compiler doesn't check for. An ArrayIndexOutOfBoundsException would be such an exception. Whenever you use the [] operator to get a value from an array, the compiler doesn't require you to handle the ArrayIndexOutOfBoundsException that could occur because this is a run-time exception.
It is still possible to catch a run-time exception, although it is not required. If you fail to do so, however, your application will terminate when/if such an exception occurs.
Simply put, checked exceptions are exceptions the compiler checks for and unchecked exceptions are exceptions that the compiler doesn't check for.
Hope that helps,
Corey


SCJP Tipline, etc.
Selvan tiru
Greenhorn

Joined: Jul 12, 2001
Posts: 18
checked exceptions are compile time exceptions:
Hence called checked exceptions (Sweet)
Java.sun.com/tutorial has the best explanation!!!
--Selvan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checked/Unchecked Exceptions
 
Similar Threads
Checked and Unchecked Exceptions
Exception handling
Checked and JVM Exceptins
Checked and uncheked exceptions
Questions from mock Exam !!!