Python - pythoncom.com_error handling in Python 3.2.2

前端 未结 2 1865
旧巷少年郎
旧巷少年郎 2021-01-14 13:05

I am using Python 3.2.2, and building a Tkinter interface to do some Active Directory updating. I am having trouble trying to handle pythoncom.com_error exceptions.

相关标签:
2条回答
  • 2021-01-14 13:21

    You simply need to use the modern except-as syntax, I think:

    import pythoncom
    import win32com
    import win32com.client
    
    location = 'fred'
    try:
        ad_obj=win32com.client.GetObject(location)
    except pythoncom.com_error as error:
        print (error)
        print (vars(error))
        print (error.args)
        hr,msg,exc,arg = error.args
    

    which produces

    (-2147221020, 'Invalid syntax', None, None)
    {'excepinfo': None, 'hresult': -2147221020, 'strerror': 'Invalid syntax', 'argerror': None}
    (-2147221020, 'Invalid syntax', None, None)
    

    for me [although I'm never sure whether the args order is really what it looks like, so I'd probably refer to the keys explicitly; someone else may know for sure.]

    0 讨论(0)
  • 2021-01-14 13:28

    I use this structure (Python 3.5) --

    try: ... except Exception as e: print ("error in level argument", e) ... else: ...

    0 讨论(0)
提交回复
热议问题