Re: exception inside constructors and finalize
- From: Ross Bamford <ross@xxxxxxxxxxxx>
- Date: Thu, 28 Apr 2005 10:29:19 +0100
On Thu, 2005-04-28 at 08:52 +0200, Tor Iver Wilhelmsen wrote:
> "sarath" <mesarath@xxxxxxxxx> writes:
>
> > 1.how can we handle exception inside constructors and finalize method?
>
> try/catch or (for constructors) throws clause - as usual.
>
> > 2.what will happen when an exception occur inside constructors and
> > finalize()?
>
> Same as happens when it occurs anywhere else: You need to pass it on
> or catch it. Since finalize() is inherited from Object, you cannot add
> any exception throws in it.
>
> > will it be properly garbage collected ?
>
> What do you mean? An exception object- like any other, will be a
> candidate for collection as soon as it's no longer used.
>
> > 3.can we call finalize method explicitly ? and what will happen then ?
>
> It's a method, so you can call it yourself. What will happen is that
> the code will be executed. Since you are calling it outside it's
> normal lifecycle use, you might screw up your program doing so, if it
> releases any resources or things like that.
I think OP meant 'happen to the Object'. Just to clear up.
An exception handled within a constructor is a regular exception, but
one thrown by a constructor *should* result in the object not being
allocated, or deallocated immediately. It *always* leaves the object in
an undefined state (Null in my experience but don't rely on it)
For example the following code:
public class Test
{
public String tester;
public Test() throws Exception
{
tester = "Tester object";
throw(new Exception());
}
public static void main(String args[])
{
Test t = null;
try {
t = new Test();
} catch (Exception e) { /* ignore */ }
String s = (t == null) ? "Null object" : t.tester;
System.out.println(s);
}
}
produces the output "Null object", unless we remove the throws clause
from the constructor (when it's "Tester object" obviously).
As for Finalize, it's not a 'destructor' - just a method Java calls when
it [feels like it and might be] about to destroy that object. You could
throw a RuntimeException from it, but the object would still be
deallocated.
Finalize is an empty method, provided by the API for you to perform
special actions at (or around) GC time. You can call it, and it will do
whatever you override it to do, but the object will not be deallocated.
You're thinking of it backwards. Instead of finalize() destroying the
Object, when the JVM destroys an object it does something like:
doGC(Object o)
{
try {
o.finalize();
} catch (RuntimeException e) {
/* ... Invoke complex error handling (i.e. ignore) ... */
}
/* ... Last check object has no references and so on ... */
/* ... Do the actual work of destroying the object ... */
}
(p.s. before everyone jumps on that, it's a SIMPLIFIED PARAPHRASED
EXAMPLE :) )
Of course, although there is no way to directly stop an Object being
deallocated from finalize(), there is a trick - simply assign a
reference to the object somewhere. That way, finalize returns, and when
the JVM does it's 'last check' it finds the reference, and the object is
retained. This is part of the reason finalize is dangerous.
Cheers,
Ross
--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + info@xxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: exception inside constructors and finalize
- From: Patricia Shanahan
- Re: exception inside constructors and finalize
- References:
- exception inside constructors and finalize
- From: sarath
- Re: exception inside constructors and finalize
- From: Tor Iver Wilhelmsen
- exception inside constructors and finalize
- Prev by Date: Re: --really newbie-- most frequent character
- Next by Date: Re: exception inside constructors and finalize
- Previous by thread: Re: exception inside constructors and finalize
- Next by thread: Re: exception inside constructors and finalize
- Index(es):
Relevant Pages
|