Thread overview
throw not a valid return
Jul 31, 2004
stonecobra
Jul 31, 2004
Sean Kelly
Jul 31, 2004
Matthew
July 31, 2004
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
July 31, 2004
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
July 31, 2004
"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.