• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is difference between Checked and Uncheched exceptions ?  RSS feed

 
Saurabh Patil
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a beginner, i hava a doubt - as java supports both checked and unchecked exceptions what is the significance of checked and unchecked exceptions, and difference between them?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Exceptions tutorial should answer your question. Post again if there's anything you're still not sure of after reading it.
 
Saurabh Patil
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the tutorial you suggested but i did not find what is checked exception and what is unchecked exception. Is it that if the exception is caught its checked exception and if its not caught then its unchecked exception??
[ August 24, 2007: Message edited by: Saurabh Patil ]
 
krishnamoorthy kitcha
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi patil

There is some difference there for checked and unchecked Exception

Suppose checked exception means it shows the compile time exception

ex: NoSuchMethod exception, NoSuch Field Exception , ClassNot Found Exception

where as in Unchecked Exception shows

Runtime Only

ex: Nullpointer Exception, Number Format Exception

For your Reference

http://faqs.javabeat.net/exception/java-exception-interview-questions-faqs-1.php
 
Sahid Khan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Did you read this ?

In java there are three kinds of Exceptions:
1. Error
2. RunTimeExceptions
3. Exceptions (but not RuntimeExceptions)

First two kinds of exceptions are called unchecked exception, whereas third kind of exception is called checked exception. The reason being, compiler forces you to anticipate/handle checked exception. So if your code throws third kind of exceptions, you must catch them or throw them explicitly.

HTH,
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the tutorial you suggested but i did not find what is checked exception and what is unchecked exception.

Did you really read it ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually that tutorial doesn't come out and define checked and unchecked very well.

A particular exception class is Checked or Unchecked depending on what class it extends.

If you throw a checked exception out of a method, you must say so on the method signature. The compiler enforces this rule.

If you call a method that throws a checked exception and says so in its signature, you must either catch the exception or let it "bubble up" out of your method. If you let it out, you have to say so on your signature again.


You are not required to list any unchecked exceptions you might throw in the method signature. And when you call a method that might throw an unchecked the compiler won't see any "throws" clause, and won't force you to catch or handle the unchecked exception. For example, lots of methods might throw NullPointerException. You can catch them or not, the compiler won't care.

Did that help?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an exception is "checked," that means the compiler checks to make sure your program does something to deal with it. If an exception is unchecked, then the compiler will let it go and not require your program to do anything with it.

Which exceptions are checked? All Exceptions (java.lang.Exception and its subclasses) are checked other than RuntimeExceptions (java.lang.RuntimeException and its subclasses).

If your code might throw a checked exception, what does your program need to do to satisfy the compiler? You can either handle the exception, or declare that the exception might be thrown. Handling the exception means using a try/catch block to deal with the exception. Declaring the exception means putting a big red warning label on your code saying, "This might throw an exception because I didn't do anything to handle it."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!