Python - pythoncom.com_error handling in Python 3.2.2

前端 未结 2 1866
旧巷少年郎
旧巷少年郎 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.]

提交回复
热议问题