Hi All, Please me with this question on garbage collection from Kahlid Mughal. The question is .. Which of these statements are true? Select all valid answers. (a) The complier will fail to compile code that explicitly tries to call the finalize method. (b) The finalize method must be declared with protected accessiblity (c) An overriding finalize method in any class can always throw checked exceptions. (d) The complier will allow code that overloads the finalize method name. (e) The body of the finalize method can only access other objects that re eligible for garbage collection. Ans is (d) .But according to me the answers are (c) and (d). The definition of finalize method in Object thorws Throwable, so we can throw checked excptions in the overridden method of the subclass if its hadled with an Throws or try-catch( because just throwing a checked exception gives compile time error). Please correct me if i am wrong ... and help me how should i be looking at options of such type.... Thanks, reshma [ March 23, 2003: Message edited by: Marilyn de Queiroz ]
As you stated earlier, the throws clause of the finalize method declares Throwable so any overriding class can indeed throw an Exception. The javadoc for the Object class states the following.
If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
I agree that answer option 'c' appears to be correct. I'll move this to the errata forum.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
posted 16 years ago
Hi Dan, I tried this following code
Gives a compile time error as expected .. Check.java:4: unreported exception java.io.IOException; must be caught or declared to be thrown throw new java.io.IOException(); ^ 1 error i guess because of this reason the choice (c) was not selected. I am really confused as to how to select the choices from multipule choice question Reshma
One of the rules of method overriding is: An overriding method cannot have less restrictive exceptions than the superclass method. As a consequence you cannot throw new or broader exceptions in the overriding method. As unhandled exceptions travel up the hierarchy a violation of this rule would result in any new exception being handled by the JVM. This is obviously not what you want. As a consequence of the above rule is option C not right.
Before writing the above code, just look at it. While throwing any exception with in the method body, it is mandatory to declare that this method "throws" xxxException, which is not found in that above code, which leads to compile time error. And if you go throught the java documentation for the finalize method(), it is clearly mentioned that "If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates." So what is the point in (c)An overriding finalize method in any class can always throw checked exceptions? Throwable is declared to notify the JVM, any unxepected exceptions it receives, ignore garbage collection, which is as good as not calling finalize() method. So to me answer is only (d). This is my perception. Any other thoughts are welcome.
posted 16 years ago
While throwing any exception with in the method body, it is mandatory to declare that this method "throws" xxxException, which is not found in that above code, which leads to compile time error.
Ya what you mentioned is right and its true when a method throws Checked Exceptipons only and not all excptions.
So what is the point in (c)An overriding finalize method in any class can always throw checked exceptions?
Ya there is no use of throwing an exception, but its not that u cannot throw one, with method overriding, the overridden methods in the child classes can throw only those checked exception or their sub types that are declared in the throws clause of the method in the parent class. Since the finalize() method in Object class throws Throwable, the subclasses are at ease to throw any type of exception.
any unxepected exceptions it receives, ignore garbage collection, which is as good as not calling finalize() method.
Based on the Java Docs, overriden method definition in the subclass can only specify all, none, or a subset of the checked exception classes (including their subclasses) specified in the throws clause of the inherited method.
So logicaly "finalize()" should not be an exception to this rule.After all the first edition of Khalid A. Mughal's book explicitly asserts: "finalize() is like any other method". (page 644 the exercise solution 8.4).
Now think about this scenario:
Two checked exceptions can be listed in the throws clause of the protected finalize() method belong to the parent class. The child class overrides the inherited finalize() method and specifies only one (or even non) of the two checked exceptions: compiler doesn't not complain.
The (c) choice can not be true because of the word "always":
If the finalize() method in the parent class doesn't throw any checked exception, then the overridden finalize()in child class can not specify any checked exception in its throws clause (based on the mentioned rule).
So to me chooice c is absolutly wrong.
What are you doing? You are supposed to be reading this tiny ad!
how do I do my own kindle-like thing - without amazon