www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Throwing exception in constructor

reply Stijn Herreman <stijn.herreman telenet.be> writes:
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)
	{
		...
	}
Aug 16 2011
next sibling parent reply Vijay Nayar <madric gmail.com> writes:
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 constructor.d(13): Not high enough. //Hambo ham1 = new Hambo("4"); Hambo ham2 = new Hambo("14"); writeln(ham2._value); }
Aug 16 2011
parent Stijn Herreman <stijn.herreman telenet.be> writes:
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: object.Exception constructor.d(13): Not high enough. //Hambo ham1 = new Hambo("4"); Hambo ham2 = new Hambo("14"); writeln(ham2._value); }

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; } } }
Aug 16 2011
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 08/16/2011 07:08 PM, 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)
 {
 ...
 }

The Exception has no influence, the following code will still get the error: class JSON{ public this(string p1, string p2, string p3) { string json = download_string(...); if (json is null) { ... } else { this(json); } } this(string json) { ... } } This is afaik by design (described somewhere in TDPL). The idea is that a constructor can either construct the value itself in all code paths or delegate construction to another constructor in all code paths. The fact that the code compiles with return but not with throw is probably a bug.
Aug 16 2011
next sibling parent Vijay Nayar <madric gmail.com> writes:
On Tue, 16 Aug 2011 19:33:06 +0200, Timon Gehr wrote:

 On 08/16/2011 07:08 PM, 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)
 {
 ...
 }

The Exception has no influence, the following code will still get the error: class JSON{ public this(string p1, string p2, string p3) { string json = download_string(...); if (json is null) { ... } else { this(json); } } this(string json) { ... } } This is afaik by design (described somewhere in TDPL). The idea is that a constructor can either construct the value itself in all code paths or delegate construction to another constructor in all code paths. The fact that the code compiles with return but not with throw is probably a bug.

Well I'll be a monkey's uncle. I tried it out and sure enough you are correct. I'm still reading TDPL, and now I'm curious to find the rationale. public this(string value) { if (value == "Hambo") { _value = 999; } else { this(to!int(value)); } }
Aug 16 2011
prev sibling parent Stijn Herreman <stijn.herreman telenet.be> writes:
On 16/08/2011 19:33, Timon Gehr wrote:
 On 08/16/2011 07:08 PM, 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)
 {
 ...
 }

The Exception has no influence, the following code will still get the error: class JSON{ public this(string p1, string p2, string p3) { string json = download_string(...); if (json is null) { ... } else { this(json); } } this(string json) { ... } } This is afaik by design (described somewhere in TDPL). The idea is that a constructor can either construct the value itself in all code paths or delegate construction to another constructor in all code paths. The fact that the code compiles with return but not with throw is probably a bug.

I don't, however, understand the reason behind this design.
Aug 16 2011