Originally posted by nik arora:
Hi Chandra,
p1.readit(); //Need not handle or declare, because //accessed using class ref variable and that says to//throw unchecked exception
I didnot get the above statement please explain
Modifiable interface readit() declares the IOException that it
throws it. IOException is the checked exception. The implementing class Program is
free to declare that exception in the method or not. But it can't
throw wider or new checked exception. What can it only throw are:
1- The same exception thrown by the interface method
2- Or subclass of it
3- Or unchecked exceptions. (any number of)
In our case we say that we will will implement the interface method readit()
following 3rd case.
Important Line:Whether the exception to be handled (or not) only the reference type matters, not the actual type of the object. [/b]
p1.readit();
p1 is the reference variable of the Program class, Program class implements the method readit() with unchecked exception. So I said we need not to declare or handle it. (Because compile can only see at the reference type,
reference type method does not throw the checked exception, so no problem of handling or declaring it). OK?
We have to bother about handling or declaring rule when the reference type is interface Modifiable; Because Modifiable says that readit() throws IOException (check exception)
Modifiable m1;
m1.readit(); //ERROR, you must handle or declare it (in our case main() method should declare it or place this statement inside the try catch )