how to stop python

victor vijayendra.bapte at gmail.com
Mon Jul 31 06:02:13 EDT 2006


or if u want explicit exit of program then use:

import sys
sys.exit(1)

or

raise SystemExit, 'message'

Dan wrote:
> bruce bedouglas at earthlink.net posted:
>
>  > perl has the concept of "die". does python have anything
>  > similar. how can a python app be stopped?
>
> I see this sort of statement a lot in Perl:
>      open(FH, "myfile.txt") or die ("Could not open file");
>
> I've no idea why you're asking for the Python equivalent to die, but if
> it's for this sort of case, you don't need it.  Usually in Python you
> don't need to explicitly check for an error.  The Python function will
> raise an exception instead of returning an error code.  If you want to
> handle the error, enclose it in a try/except block.  But if you just
> want the program to abort with an error message so that you don't get
> silent failure, it will happen automatically if you don't catch the
> exception.  So the equivalent Python example looks something like this:
>      fh = file("myfile.txt")
>
> If the file doesn't exist, and you don't catch the exception, you get
> something like this:
> $ ./foo.py
> Traceback (most recent call last):
>    File "./foo.py", line 3, in ?
>      fh = file("myfile.txt")
> IOError: [Errno 2] No such file or directory: 'myfile.txt'
> 
> /Dan




More information about the Python-list mailing list