How can I more easily suppress previous exceptions when I raise my own exception in response?

后端 未结 2 1688
北恋
北恋 2020-12-06 04:19

Consider

try:
   import someProprietaryModule
except ImportError:
   raise ImportError(\'It appears that  is not installed...\')         


        
2条回答
  •  无人及你
    2020-12-06 04:53

    In Python 3.3 and later raise ... from None may be used in this situation.

    try:
       import someProprietaryModule
    except ImportError:
       raise ImportError('It appears that  is not installed...') from None
    

    This has the desired results.

提交回复
热议问题