Is there a way to decode numerical COM error-codes in pywin32

前端 未结 5 1062
别那么骄傲
别那么骄傲 2020-12-28 15:26

Here is part of a stack-trace from a recent run of an unreliable application written in Python which controls another application written in Excel:

pywintype         


        
5条回答
  •  粉色の甜心
    2020-12-28 15:50

    No-one has yet mentioned the strerror attribute of the pywintypes.com_error Exception. This returns the result of FormatMessage for the error code. So instead of doing it yourself like this

    try:
        [whatever code]
    except pythoncom.com_error as error:
        print(win32api.FormatMessage(error.excepinfo[5]))
    

    You can just do this:

    try:
        [whatever code]
    except pythoncom.com_error as error:
        print(error.strerror)
    

    Note it will return None if you have a non-standard HRESULT :(

提交回复
热议问题