On 16/08/2011 19:24, Vijay Nayar wrote:
On Tue, 16 Aug 2011 19:08:53 +0200, Stijn Herreman wrote:

Why is the return statement required, while nothing after the Exception
is executed?
Error: one path skips constructor

        public this(string p1, string p2, string p3) {
                string json = download_string(...);
                
                if (json is null)
                {
                        throw new Exception("404");
                        writeln("test");
                        return;
                }
                else
                {
                        this(json);
                }
        }
        
        package this(string json)
        {
                ...
        }

What compiler are you using?  Using DMD2 (DMD32 D Compiler v2.054), I
have no problem with the example below.  I suspect the problem may not
have anything to do with return statements.

import std.stdio;
import std.conv;

class Hambo {
     public int _value;

     public this(string value) {
         this(to!int(value));
     }

     public this(int value) {
         if (value<  10) {
             throw new Exception("Not high enough.");
         } else {
             _value = value;
         }
     }
}

void main() {
     // Throws: [email protected](13): Not high enough.
     //Hambo ham1 = new Hambo("4");
     Hambo ham2 = new Hambo("14");
     writeln(ham2._value);
}
It happens only when chaining constructors.

Doesn't compile:
class Hambo {
    public int _value;

    public this(string value) {
                if (value == "") {
                        throw new Exception("Empty.");
                } else {
                        this(to!int(value));
                }
    }

    public this(int value) {
        if (value < 10) {
            throw new Exception("Not high enough.");
        } else {
            _value = value;
        }
    }
}

Compiles:
class Hambo {
    public int _value;

    public this(string value) {
                if (value == "") {
                        throw new Exception("Empty.");
                        return;
                } else {
                        this(to!int(value));
                }
    }

    public this(int value) {
        if (value < 10) {
            throw new Exception("Not high enough.");
        } else {
            _value = value;
        }
    }
}

Compiles:
class Hambo {
    public int _value;

    public this(string value) {
                if (value == "") {
                        throw new Exception("Empty.");
                }
    }

    public this(int value) {
        if (value < 10) {
            throw new Exception("Not high enough.");
        } else {
            _value = value;
        }
    }
}

Reply via email to