digitalmars.D.learn - statement is not reachable
- Qian Xu (33/33) Mar 05 2009 Hi All,
- Jarrett Billingsley (4/17) Mar 05 2009 This line is unreachable. The 'throw' before it makes it impossible
- Qian Xu (5/24) Mar 05 2009 Hi Jarrett,
- Ary Borenszweig (26/54) Mar 05 2009 No instance is created if you throw an exception. Or... I don't know
- Jarrett Billingsley (10/13) Mar 05 2009 By the time the constructor runs, an instance has already been
Hi All, I have got a warning by compiling ("gdc file -Wall") the following code ---------------------------------------------------------- module unreachable; class Foo { this(int i) {} this(char[] s) {} 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 } } void main() { Foo foo = new Foo("world", 1); assert(false, "END"); } ----------------------------------------------------------- The warning is: "statement is not reachable" If I use switch-case-statment, I can see even an error message: "constructor calls not allowed in loops or after labels" So, is my design incorrect, or is the compiler too strict? --Qian Xu
Mar 05 2009
On Thu, Mar 5, 2009 at 3:17 AM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote= :=A0this(char[] s, int flag) { =A0 =A0if (flag =3D=3D 1) =A0 =A0{ =A0 =A0 =A0this(1); =A0 =A0 =A0return; =A0 =A0} =A0 =A0else if (flag =3D=3D 2) =A0 =A0{ =A0 =A0 =A0this("hello"); =A0 =A0 =A0return; =A0 =A0} =A0 =A0throw new Exception("unhandled case"); =A0 =A0this(0); // fakeThis line is unreachable. The 'throw' before it makes it impossible for it to ever execute. Just take out this line.
Mar 05 2009
Jarrett Billingsley wrote:On Thu, Mar 5, 2009 at 3:17 AM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:Hi Jarrett, but I need an exception here. This is an unexpected case. I want no instance to be create in this case. --Qianthis(char[] s, int flag) { if (flag == 1) { this(1); return; } else if (flag == 2) { this("hello"); return; } throw new Exception("unhandled case"); this(0); // fakeThis line is unreachable. The 'throw' before it makes it impossible for it to ever execute. Just take out this line.
Mar 05 2009
Qian Xu wrote:Jarrett Billingsley wrote: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"); } } }On Thu, Mar 5, 2009 at 3:17 AM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:Hi Jarrett, but I need an exception here. This is an unexpected case. I want no instance to be create in this case. --Qianthis(char[] s, int flag) { if (flag == 1) { this(1); return; } else if (flag == 2) { this("hello"); return; } throw new Exception("unhandled case"); this(0); // fakeThis line is unreachable. The 'throw' before it makes it impossible for it to ever execute. Just take out this line.
Mar 05 2009
On Thu, Mar 5, 2009 at 10:45 AM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:Hi Jarrett, but I need an exception here. This is an unexpected case. I want no instance to be create in this case.By the time the constructor runs, an instance has already been created. But if you throw an exception in the ctor, it's impossible for a reference to that instance to be stored anywhere, unless you store it within the ctor itself, i.e. this() { someGlobal = this; throw new Exception("foo!"); }
Mar 05 2009