wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Checked and 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 and Unchecked exceptions " Watch "Checked and Unchecked exceptions " New topic
Author

Checked and Unchecked exceptions

Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
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

Joined: Apr 17, 2006
Posts: 8
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

Joined: May 18, 2010
Posts: 5
Perfect Explanation guys
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: May 17, 2010
Posts: 65
hey thanks everyone for your answers...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checked and Unchecked exceptions
 
Similar Threads
Parsing Problem
Exception handling
Checked Vs Unchecked exceptions
Checked and uncheked exceptions
Questions from mock Exam !!!