Overriding and throws clause
Overriding and throws clause
Ans: Yes
public static void main(String[] args) {
MyType myType = new MyType();
myType=null; // Line 1
java.util.ArrayList arrayList = new java.util.ArrayList(); //line 5
while(true) {
arrayList.add(new String("waste"));
}
}
}
class MyType{
protected void finalize() throws Exception{
System.out.println("bye");
}
}
Let's look again at the second and third sentences you quoted: "Overriding methods can limit the range of throwables to unchecked exceptions. Further Overridden definitions of this method in subclasses will not be able to throw checked exceptions." Now I assumed that these two sentences went together. That is when they talk about "further overridden definitions", they mean further after you've already overridden with a method that limits the range of throwables to unchecked exceptions. You haven't done that, so you're getting different results than they describe.
Try this:
class Foo {
protected void finalize() {
System.out.println("finalize in Foo");
}
}
class Bar extends Foo {
protected void finalize() throws Exception {
System.out.println("finalize in Bar");
}
}
0 Comments:
Post a Comment
<< Home