www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - throw not a valid return

reply stonecobra <scott stonecobra.com> writes:
Given the code:

class ThrowIt {
   public bool toss(Object o) {
     throw new Exception("msg");
   }
}

DMD gives:

C:\usr\local\src\d>dmd throw.d
throw.d(2): function toss function expected to return a value of type bit

Adding a:

return false;

after the throw solves the problem.  Shouldn't the throw be considered a 
return?

Scott Sanders
Jul 30 2004
parent reply Sean Kelly <sean f4.ca> writes:
stonecobra wrote:
 Given the code:
 
 class ThrowIt {
   public bool toss(Object o) {
     throw new Exception("msg");
   }
 }
 
 DMD gives:
 
 C:\usr\local\src\d>dmd throw.d
 throw.d(2): function toss function expected to return a value of type bit
 
 Adding a:
 
 return false;
 
 after the throw solves the problem.  Shouldn't the throw be considered a 
 return?

Jill brought this up a week or so ago. Currently, every function is required to have one return statement somewhere, though there was some consideration given to dropping the restriction. I personally like the idea that every function is required to have at least one valid execution path, though this ignores the possibility of stubs that should never be called, etc. Sean
Jul 30 2004
parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:ceerr8$d0t$1 digitaldaemon.com...
 stonecobra wrote:
 Given the code:

 class ThrowIt {
   public bool toss(Object o) {
     throw new Exception("msg");
   }
 }

 DMD gives:

 C:\usr\local\src\d>dmd throw.d
 throw.d(2): function toss function expected to return a value of type bit

 Adding a:

 return false;

 after the throw solves the problem.  Shouldn't the throw be considered a
 return?

Jill brought this up a week or so ago. Currently, every function is required to have one return statement somewhere, though there was some consideration given to dropping the restriction. I personally like the idea that every function is required to have at least one valid execution path, though this ignores the possibility of stubs that should never be called, etc.

Stubs that should never be called indicate a design flaw, or at best a design compromise. As such, I think it's quite appropriate to require the return type if only to draw attention to this fact.
Jul 31 2004