Throwing exception in constructor
Vijay Nayar
madric at gmail.com
Tue Aug 16 10:24:01 PDT 2011
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: object.Exception at constructor.d(13): Not high enough.
//Hambo ham1 = new Hambo("4");
Hambo ham2 = new Hambo("14");
writeln(ham2._value);
}
More information about the Digitalmars-d-learn
mailing list