Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Diff between checked exception & unchecked exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Diff between checked exception & unchecked exception" Watch "Diff between checked exception & unchecked exception" New topic

Diff between checked exception & unchecked exception

Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Can anybody give me the detailed, elborated difference between checked exception and unchecked exception.As per my knowledge is concernerd i know this much:the checked exceptionare checked at compile time and unchecked at runctime. and one more thing unchecked exception requeries try & catch block.
Pls guide correct me if i am wrong.I got this doubt when i was doing this
public class CheckValues
public static void main(String[] args)
int a=100;
a /=0;
System.out.println("the value of a " + a);
1. Compile time error : ArithmeticException / by zero
2. Run time error: ArithmeticException / by zero.

girish hegde

Joined: Oct 27, 2000
Posts: 20
i don't know what kind of a compiler you are using.
when you compile this code it will compile without any problem.
but when you run it an ArithmeticException will be thrown.
there are two kinds of exceptions.
One which come from Exception class
and another which come from RuntimeException
those which come from Exception are called checked exception i.e the compiler checks if they are properly caught or whatever.
But those which come from RuntimeException occur only at runtime.
It is bad practise to catch RuntimeExceptions.
one should see that they are avoided in the program.
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi prasad,
The above code given by you generates only RuntimeException.(they are also called unchecked exception since they need not be checked.)
Runtime exceptions need not be caught(hence no try/catch block)
You have to use either try/catch block or throws clause in method declaration for checked exceptions. The compiler checks whether you are using try/catch for checked exceptions or not. If do not use any one of above 2 methods the compiler will generate error.
You can read RHE certification book page no 147 for further reading.

[This message has been edited by P SOLAIAPPAN (edited October 29, 2000).]
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Hi solaiappan.
Thanks for your guidence.
Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
I think unchecked exceptions occur are the erros in your program and every one knows its not good to have bugs.
The checked exceptions are actually objects of the subclasses of the java.lang.Exception class.
Danish Shaukat
I agree. Here's the link:
subject: Diff between checked exception & unchecked exception
It's not a secret anymore!