Hi all,

I came across something a little odd today.  I normally have various signatures
to my object constructors, and I usually write one and then use this() from
within the others to make minor changes.  Like below.

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

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

This works great and keeps me from having to duplicate code.  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).  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.

In any case, the compiler (Blackdown 1.3.1 FCS) then returned with an error
saying that the call to 'this' must be the first statement in the constructor
(apparently the 'try' is now the first statement and this isn't allowed).

Can someone explain a bit more what's going on here?  Is 'try' a 'statement'?
If there is a reason for this kind of error other than the simple , "if the
first token of the constructor body is checked and if it's not 'this' and 'this'
is seen later, throw an exception", kind of thing I'd be interested in knowing
what it is.

I ended duplicating code since I *really* didn't want to throw that type of
exception.  Any suggestions how this might be handled in the future.  Any chance
this 'this' restriction will be relaxed in further compilers to allow for
exception catching?

Thanks,
Jim


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

Reply via email to