Forums Register Login

Diff between checked exception & unchecked exception

+Pie Number of slices to send: Send
hi,
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.
Regards
Prasad

------------------
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
solaiappan.

[This message has been edited by P SOLAIAPPAN (edited October 29, 2000).]
+Pie Number of slices to send: Send
Hi solaiappan.
Thanks for your guidence.
Prasad
------------------
+Pie Number of slices to send: Send
Hi!
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.
Regards
Danish Shaukat
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 635 times.
Similar Threads
Regarding Exception Catching
NumberFormatException
Usage of Exceptions with Overriding
Method overriding
IOException confusion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:39:42.