Maybe you can define it, it may even compile. But the exception will never be handled, it will in fact be silently eaten and ignored.
Very bad practice to attempt to throw anything from finalize(). In fact, you shouldn't really use finalize() at all as there's no guarantee it'll ever be executed (and even if, no guarantee as the when).
---------------------------------------------------------- Maybe you can define it, it may even compile. But the exception will never be handled, it will in fact be silently eaten and ignored.
Originally posted by vidya sagar: ---------------------------------------------------------- Maybe you can define it, it may even compile. But the exception will never be handled, it will in fact be silently eaten and ignored.
how compiler will silently eat and ignore??? (is the food like by compiler ya)
its possible to throw exception in a overridden method in any derived class because Object class has a finalise() method with following syntax
protected void finalize() throws Throwable
check in java API
Hi Vidya Sagar,
According to Java specifications, the finalize() method can throw exception that is any sub class of Throwable (or Throwable itself). The garbage collector is what normally calls finalize() method (you won't normally be calling finalize() from your code explicitly), and the garbage collector does indeed ignore any exception thrown by the finalize() method and continues with its work.
It is better to state that , compiler does look at allowing the exception to be handled by the runtime system, so in principle it does not prevent one from throwing exceptions in finalize
Originally posted by Saurabh Khanna:
Hi Vidya Sagar,
According to Java specifications, the finalize() method can throw exception that is any sub class of Throwable (or Throwable itself). The garbage collector is what normally calls finalize() method (you won't normally be calling finalize() from your code explicitly), and the garbage collector does indeed ignore any exception thrown by the finalize() method and continues with its work.