File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Checked Exceptions??? 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 "Checked Exceptions???" Watch "Checked Exceptions???" New topic

Checked Exceptions???


Joined: Jun 08, 2000
Posts: 28
Hi Every1,
I would like to know what are checked Exceptions.I had studied some of the books but not yet get the complete idea of checked Exceptions...
Could anyone of you please let me know about checked exceptions?
Thanks and Regards
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Checked exceptions are compile time exceptions. You should,must,
will catch them in your code. IOException is the best example
whenever you perform a read or write operation there is every
possiblirty of a IoException being raised. On the other hand
runtime exceptions depends on the state of execution like
Arithmetic Exception which will occur depending the values at
runtime. That cannot be guessed at compile time.
It is just java's way of informing you that "this operation which
you are doing may result in a exception, Do you know that?."
You should tell yes by a try catch block or defining the throws
keyword in the method signature.
Hope this helps

V. Kishan Kumar

Regards,<BR>V. Kishan Kumar
Peter Lyons
Ranch Hand

Joined: Sep 12, 2000
Posts: 202
I'll pass along what I found in Mughal & Rasmussen's "Programmers' Guide to Java Certification" which in my opinion is an excellent book.
"Except for RuntimeException, Error and their subclasses, all exceptions are called checked exceptions. The compiler ensures that if a method can throw a checked exception, directly or indirectly, then the mehtod must explicitly deal with it. The method must either catch the exception and take the appropriate action, or pass on the exception to its caller.
(here there is a reference to p.162, which states:
Any method that can cause a checked exception to be thrown, either directly by using the throws statement or indirectly by invoking other methods which can throw this exception , must deal with the exception in, usually, one of two ways. Either it can use a try block and catch the exception in a handler and deal with it, or it can explicitly propagate the exception to its caller by using the throws statement in its method header. This mechanism ensured that, regardless of the path of execution, a checked exception will be monitored.)
continuing on page 155 from where we started:
Exceptions defined by Error and RuntimeException classes and their subclasses are known as unchecked exceptions, meaning that method is not obligated to deal with these kinds of exceptions."
I thought if I tried to re-state all of that something would be missed. Bottom line, I guess, is that a checked exception requires explicit handling. You can't ignore it in your code.
[This message has been edited by Peter Lyons (edited October 11, 2000).]
Mapraputa Is
Leverager of our synergies

Joined: Aug 26, 2000
Posts: 10065
Hi Sunita!
Plus to all what Kishan and Peter said:
Here is exception hierarhy and it�s easy to see what we usually miss while studying exceptions � Throwable class.

I met a question about exception of Throwable class on one of mocks and after having been unable to answer constructed this question for later revision:
Which method being inserted will NOT cause compile-time error:

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
I think I know why it is c and d, but I wanted to my post my thoughts to see if I am missing something.
One of the rules for overriding methods is that you can not throw CHECKED exceptions that were not thrown in the base class, unless the checked exception you are throwing is a subclass of the checked exception thrown in the base class.
So for your example, since the base class throws no exceptions, you cannot throw any checked exceptions either. C and D are not checked exceptions, so these are allowed. Throwable is the base of all the exceptions, so it has checked exceptions as a subclass, so that is why that is A will give a compile error.

Joined: Jun 08, 2000
Posts: 28
Thanks for all who has tried to give me idea about checked exceptions.
But I have another doubt,i.e., in the example given by Mapraputa why option b is not correct as exception is also one of the subclasses of Throwable.
And in bill's reply I think that checked Exceptions rule is also applied for unchecked exceptions.
Thanks and Regards
Mukesh Rathod
Ranch Hand

Joined: Oct 11, 2000
Posts: 32
The option b is not correct. As mentioned by bill, the rule of overriding method says that the overriding method can not throw any Checked Exception which are not thrown by the overridden method. It may only throw the checked exception thrown by the overridden method or the sub class of that checked exception. One more thing, the overriding method may or may not throw an exceptioon thrown by the overridden method. i.e.
class A{
void aMethod () throws IOException{}
class A extends A{
// void aMethod here.
following r legal :
void aMethod() throws IOException{}
void aMethod() throws FileNotFoundException()
void aMethod(){}
Note that the rules are not applicable for unchecked exceptions i.e. RuntimeExceptions. Also the same is not applicable for over loading methods.
Hope this clarifies.
I agree. Here's the link:
subject: Checked Exceptions???
It's not a secret anymore!