• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checked and Unchecked exceptions

 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between checked and unchecked exceptions...please explain me in a bit detail coz i didn't understand it when i read it from a few books.....
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Checked exceptions are exceptions for which the compiler checks if you handle them properly. If you don't catch a checked exception, or if you don't add a throws clause to a method in which a checked exception can occur, the compiler will give you an error.

Unchecked exceptions are exceptions which are not checked by the compiler: you do not need to catch them or specify that your method throws them. If an unchecked exception happens in your application, it's usually a sign that there is a bug in the code. Most of the time it doesn't make a lot of sense to try and catch an unchecked exception - instead, you should just look at the source code and fix the bug.

Classes that extend java.lang.Exception are checked exceptions, except classes that extend java.lang.RuntimeException - those are unchecked exceptions.

Examples of checked exceptions are IOException and SQLException.

Examples of unchecked exceptions are NullPointerException, UnsupportedOperationException, ClassCastException.

See also Exceptions in Sun's Java Tutorials.
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your answer...
If am given the name of an Exception class...how do i decide that it's a checked or unchecked exception just by looking at it's name???
 
Kamlesh Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule for identifying whether an exception is checked or un-checked:

Classes that extend java.lang.Exception are checked exceptions, except classes that extend java.lang.RuntimeException - those are unchecked exceptions.


as mentioned by Jesper.
 
Mkunal badgujar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect Explanation guys
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As well as something extending RuntimeException, anything extending java.lang.Error also is treated as if it were an unchecked Exception.
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks everyone for your answers...
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic