I don't know enough about when and when not this() is allowed.  But
from my basic understanding of exceptions, and after some coding
exercise, I am confused.

First, you have:

Jim Hazen <[EMAIL PROTECTED]> writes:

> ExampleService(String host, int timeout)
> {//impl here}

This signature requires that this constructor will not throw any
checked exceptions.

Then, you say:

> However I ran into
> the case today where one of the arguments to my this() call could throw an
> exception (in my case I'm getting the InetAddress of the localhost and it can
> throw an UnknownHostException).

This is a checked exception.  Didn't the compiler complain about
ExampleService(String host, int timeout) itself in the first place?
It should.

Perhaps in your actual code you do write:

  ExampleService(String host, int timeout) throws UnknownHostException

in which case I partially understand your case.

Then you indicate your intention to hide UnknownHostException and
throw your own --- let's call it ExampleException here:

> If I include this argument as is, the compiler
> will complain about the exception not being rethrown or enclosed in a try/catch
> block.  Fine, I enclose it in a try/catch block and rethrow an exception more
> suitable to my application than UnknownHost, which wouldn't make any real sense
> in this context unless you know what was going on under the covers.

Apparently you want to do this just in the other constructor
ExampleService(String host), i.e., the first constructor throws
UnknownHostException and the second throws ExampleException:

  ExampleService(String host, int timeout) throws UnknownHostException
  ExampleService(String host) throws ExampleException

This looks odd, and it is difficult to do --- can't write
"try { this(host, 0); } catch ..." in the second constructor.

But have you considered having *both* constructors throw the same
ExampleException?  Then it is a uniform design (all constructors have
comparable semantics), it maximizes code reuse, and it is
straightforward to implement: do your try-catch-rethrow in just
one place, the first constructor:

  ExampleService(String host, int timeout) throws ExampleException
  {
    try {
      // impl here
    }
    catch (UnknownHostException e) {
      throw new ExampleException();
    }
  }

  ExampleService(String host) throws ExampleException
  {
    this(host, 0);
  }

If this doesn't suit you, I don't have any suggestion.  I have a feeling
that the restriction about "try { this(...); }" is not going away soon.
For that matter, "if (1==1) { this(...); }" is forbidden too.  To find
out the exact restrictions on this(...) requires reading the Java language
specification carefully (and I haven't done so).

Actually, I have a suggestion in case the above doesn't suit you, and I
think this is why the authors of Java do not feel the need to relax
the restrictions, since you can always do this:

  private void mySecretCtor(String host, int timeout) throws UnknownHostException
  {
    //ctor impl here
  }

  ExampleService(String host, int timeout) throws UnknownHostException
  {
    mySecretCtor(host, timeout);
  }

  ExampleService(String host) throws ExampleException
  {
    try {
      mySecretCtor(host, 0);
    }
    catch (UnknownHostException e) {
      throw new ExampleException();
    }
  }

Hope this now helps.


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to