Constructors, exceptions, and instances.

Discussion in 'Java' started by Carl Howells, Jun 4, 2004.

  1. Carl Howells

    Carl Howells Guest

    This is such a bad idea. And yet, it works:

    --- code ----
    public class Test
    {
    private int b;

    public Test(int a) throws Test.HoldingException
    {
    b = a;
    throw new HoldingException(this);
    }

    public static void main(String [] args)
    {
    try
    {
    Test s = new Test(10);
    }
    catch (Test.HoldingException e)
    {
    Test s = (Test)e.getHeld();
    System.out.println(s.toString() + " " + s.b);
    }
    }

    private static class HoldingException extends Exception
    {
    Object held;

    public HoldingException(Object o)
    {
    held = o;
    }

    public Object getHeld()
    {
    return held;
    }
    }
    }
    --- code ----

    I'm kind of sad that this works. At least everyone who sees it should
    know it's a bad idea.
     
    Carl Howells, Jun 4, 2004
    #1
    1. Advertising

  2. Carl Howells

    GaryM Guest

    Carl Howells <> wrote in
    news::

    > I'm kind of sad that this works. At least everyone who sees it
    > should know it's a bad idea.
    >
    >


    I'm probably slow here, but why is it sad? Because the object get's
    constructed despite thrown error in the constructor?
     
    GaryM, Jun 4, 2004
    #2
    1. Advertising

  3. Carl Howells

    Carl Howells Guest

    GaryM wrote:
    > Carl Howells <> wrote in
    > news::
    >
    >>I'm kind of sad that this works. At least everyone who sees it
    >>should know it's a bad idea.
    >>
    >>

    >
    >
    > I'm probably slow here, but why is it sad? Because the object get's
    > constructed despite thrown error in the constructor?


    It just feels very wrong to have the constructor throw an exception, yet
    still have the created instance be reachable. I realize there's nothing
    in the language spec which says it can't or shouldn't happen that way...

    But it seems like a very BAD design, no matter what.
     
    Carl Howells, Jun 4, 2004
    #3
  4. Carl Howells

    GaryM Guest

    Carl Howells <> wrote in
    news::

    > It just feels very wrong to have the constructor throw an
    > exception, yet still have the created instance be reachable. I
    > realize there's nothing in the language spec which says it can't
    > or shouldn't happen that way...
    >
    > But it seems like a very BAD design, no matter what.
    >


    IIRC, an exception in the constructor causes the object to be
    immediately eligble for gc. However this does not apply if a
    reference is held to the instance. This is an excellent thread I
    recalled reading some time ago:

    http://groups.google.com/groups?hl=...06020851.5b9c72d1%40posting.google.com&rnum=9
     
    GaryM, Jun 4, 2004
    #4
  5. Carl Howells

    Tony Morris Guest

    > It just feels very wrong to have the constructor throw an exception, yet
    > still have the created instance be reachable. I realize there's nothing
    > in the language spec which says it can't or shouldn't happen that way...
    >
    > But it seems like a very BAD design, no matter what.


    Your reasoning is flawed.

    Object o;
    try
    {
    // This could throw an exception, in which case, o isn't assigned.
    // So what?
    // A red herring, until proven otherwise.
    o = new SomeClass();
    }
    catch(SomeCheckedException e)
    {

    }


    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Jun 4, 2004
    #5
  6. "GaryM" <> wrote in message
    news:Xns94FDD9081BC44R3344324357788499939@216.168.3.44...
    > Carl Howells <> wrote in
    > news::
    >
    > > It just feels very wrong to have the constructor throw an
    > > exception, yet still have the created instance be reachable. I
    > > realize there's nothing in the language spec which says it can't
    > > or shouldn't happen that way...
    > >
    > > But it seems like a very BAD design, no matter what.
    > >

    >
    > IIRC, an exception in the constructor causes the object to be
    > immediately eligble for gc.

    Only because there will, in general, be no references to it.

    > However this does not apply if a
    > reference is held to the instance.

    Precisely.

    Here's an example even uglier than Carl's; the partially constructed object
    is resurrected by its finalizer:

    public class Ugly {
    public static Ugly ugly;

    public void create() {
    new Ugly();
    }

    Ugly() {
    throw new RuntimeException();
    }

    protected void finalize() {
    ugly = this;
    }
    }
     
    Mike Schilling, Jun 4, 2004
    #6
  7. Mike Schilling wrote:
    > "GaryM" <> wrote in message
    > news:Xns94FDD9081BC44R3344324357788499939@216.168.3.44...
    >
    >>Carl Howells <> wrote in
    >>news::
    >>
    >>
    >>>It just feels very wrong to have the constructor throw an
    >>>exception, yet still have the created instance be reachable. I
    >>>realize there's nothing in the language spec which says it can't
    >>>or shouldn't happen that way...
    >>>
    >>>But it seems like a very BAD design, no matter what.
    >>>

    >>
    >>IIRC, an exception in the constructor causes the object to be
    >>immediately eligble for gc.

    >
    > Only because there will, in general, be no references to it.
    >
    >
    >>However this does not apply if a
    >>reference is held to the instance.

    >
    > Precisely.
    >
    > Here's an example even uglier than Carl's; the partially constructed object
    > is resurrected by its finalizer:
    >
    > public class Ugly {
    > public static Ugly ugly;
    >
    > public void create() {
    > new Ugly();
    > }
    >
    > Ugly() {
    > throw new RuntimeException();
    > }
    >
    > protected void finalize() {
    > ugly = this;
    > }
    > }
    >
    >


    Whoa, that's beautiful. I'd like to think that all of these constructs
    are merely academic; that no sane programmer would ever come up with
    this. Sadly, I have a nagging feeling that somewhere, somehow, someone
    has come up with something similar and said "Hey, what a great idea!"
    This particular example serves as a good case point for the advice:
    Avoid finalizers (Bloch, Effective Java p. 20)

    Derek
     
    Derek Chen-Becker, Jun 4, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,396
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    621
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    642
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    410
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    495
Loading...

Share This Page