statement is not reachable

Ary Borenszweig ary at esperanto.org.ar
Thu Mar 5 08:07:42 PST 2009


Qian Xu wrote:
> Jarrett Billingsley wrote:
> 
>> On Thu, Mar 5, 2009 at 3:17 AM, Qian Xu <quian.xu at stud.tu-ilmenau.de>
>> wrote:
>>
>>> this(char[] s, int flag) {
>>> if (flag == 1)
>>> {
>>> this(1);
>>> return;
>>> }
>>> else if (flag == 2)
>>> {
>>> this("hello");
>>> return;
>>> }
>>> throw new Exception("unhandled case");
>>> this(0); // fake
>> This line is unreachable.  The 'throw' before it makes it impossible
>> for it to ever execute.  Just take out this line.
> 
> 
> Hi Jarrett, 
> 
> but I need an exception here. This is an unexpected case. I want no instance
> to be create in this case.
> 
> --Qian

No instance is created if you throw an exception. Or... I don't know 
what's the difference between that and trying to create an instance and 
throwing.

I'd suggest you do it like this:

module unreachable;
class Foo {
   this(int i) {}
   this(char[] s) {}
   this(char[] s, int flag)
   in
   {
     assert(flag == 1 || flag == 2);	
   }
   body
   {
     if (flag == 1)
     {
       this(1);
     }
     else if (flag == 2)
     {
       this("hello");
     }
   }
}


More information about the Digitalmars-d-learn mailing list