Catching class exceptions

Simon Foster simon at uggs.demon.co.uk
Sun Nov 12 15:42:19 EST 2000


I,m trying to catch the GetoptError class exception using code like
the code below.  This is basically a 'cut-and-paste' from the
documentation for getopt:

import getopt, sys

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help",
"output="])
    except getopt.GetoptError:
        # print help information and exit:
        print 'Getopt exception caught'
        # usage()
        sys.exit()
    output = None
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("-o", "--output"):
            output = a
    # ...

if __name__ == "__main__":
    print 'Entering main...'
    main()

When I run this program I get :

Entering main...
Traceback (most recent call last):
  File "C:\Program
Files\Python20\Pythonwin\pywin\framework\scriptutils.py", line 301, in
RunScript
    exec codeObject in __main__.__dict__
  File "H:\bin\getoptions.py", line 22, in ?
    main()
  File "H:\bin\getoptions.py", line 6, in main
    except getopt.GetoptError:
AttributeError: GetoptError
>>> 

What am I doing wrong?  Is there a special syntax for class errors? Is
the documentation for the getopt module out of date?





More information about the Python-list mailing list