Thread: quit program

    #1
  1. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27

    quit program


    How do i just quit out of the python program i.e. end it say when an exception occurs.

    try:

    open("file", "r")

    except(IOError):

    stop executing code here / end program / kill program


    Thanks
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    import sys
    sys.exit()

    Comments on this post

    • Civ247 agrees : thanks
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I prefer to raise SystemExit instead of sys.exit(), simply because it looks cleaner and is available without importing the sys module.

    Code:
    raise SystemExit
    Note: this can be caught using a try-except block like any other exception.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Does raise SystemExit simply exit the program "without any questions"?
  8. #5
  9. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    Thanks, can someone proxy rep netytan for me?

IMN logo majestic logo threadwatch logo seochat tools logo