Generator return() and exceptions

Andy Wingo wingo at
Wed Jul 23 01:28:52 PDT 2014


At one point I suggested privately that the return() method should throw
a specified exception instead of causing a "return".  The TC39 notes
from the last meeting do not record any discussion of this.

I understand that one of the concerns about this approach was about
guards and catch blocks, but making return() throw an exception does not
affect that in the least: it's already the case that any catch block may
see any old exception.  Adding a new kind of exception doesn't affect
that; properly written catch blocks have to test the positive presence
of the exception they are looking for, not the absence of exceptions
they aren't interested in.  Magical return() is no better here because,
as you note, return can be caught too.

There are valid cases in which you can have a catch without a guard;
these are when can enumerate the possible exceptions that a part of your
program will throw.  However this condition necessarily limits the size
of the try{} block -- it is unlikely to contain a yield.  So no problem
there either.

Magical returns seem unnecessary to me.


More information about the es-discuss mailing list