Catching class exceptions

Simon Foster simon at
Sun Nov 12 21:42:19 CET 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():
        opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help",
    except getopt.GetoptError:
        # print help information and exit:
        print 'Getopt exception caught'
        # usage()
    output = None
    for o, a in opts:
        if o in ("-h", "--help"):
        if o in ("-o", "--output"):
            output = a
    # ...

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

When I run this program I get :

Entering main...
Traceback (most recent call last):
  File "C:\Program
Files\Python20\Pythonwin\pywin\framework\", line 301, in
    exec codeObject in __main__.__dict__
  File "H:\bin\", line 22, in ?
  File "H:\bin\", 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