Re: exception inside constructors and finalize



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


.



Relevant Pages

  • Re: Controlled types and exception safety
    ... but it's a bug to let Finalize or Adjust propagate an exception. ... operations for an object does not corrupt any other object, ...
    (comp.lang.ada)
  • Re: Problems with controlled types, gnatmem thinks handle is leaking memory (long)
    ... No need to check for the reference count to be 0, ... I suppose I could raise an exception if the reference count isn't zero on ... finalize subprograms have been implemented correctly. ... (The compiler will finalize them anyway so you'll get an ...
    (comp.lang.ada)
  • Re: exception inside constructors and finalize
    ... > 1.how can we handle exception inside constructors and finalize method? ... try/catch or (for constructors) throws clause - as usual. ...
    (comp.lang.java.help)
  • Re: Killing an instance?
    ... >program did not get to clean up due to an exception? ... are you confusing finally with finalize? ... Canadian Mind Products, Roedy Green. ...
    (comp.lang.java.programmer)
  • Re: type checking
    ... >it's not far from your favourite SWIG-supported language. ... when its constructor raised an exception. ... It hasn't really been an issue so far, but I guess C# constructors ... This is one issue I already have with Java/C# style garbage collection ...
    (comp.lang.python)