how to stop a function execution like...

mzdude jsanga at cox.net
Tue Jun 16 09:43:55 EDT 2009


On Jun 16, 7:30 am, Gaudha <sanal.vik... at gmail.com> wrote:
> Is there any built-in function to stop execution of a function similar
> to stop the program execution by sys.exit?
> In the example below, I want to skip statement 2... if the 'if'
> condition is satisfied.
> Don't advice me to put statement 2 in 'else' block. That's not my
> intention.
> May be this a simple task. Sorry to say I'm novice in Python,
> gentlemen...
>
> def funct :
>     if (.....) : statement 1
>     statement 2

sys.exit is a pretty harsh way to stop execution. It usually
means unable to continue. There is nothing that stops you
from putting that in a function.

Another possiblity would be
def funct :
   if( .... ) :
      statement 1
      raise UserWarning, "precondition X in funct not met"

    statement 2
    ...
    statement n

Check out the try / except docs.



More information about the Python-list mailing list