Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checked/Unchecked Exceptions

 
Roberto Diaz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Selvan tiru
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
checked exceptions are compile time exceptions:
Hence called checked exceptions (Sweet)
Java.sun.com/tutorial has the best explanation!!!
--Selvan
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic