Python 如何在excepts子句中使用来自包的自定义异常?

Python 如何在excepts子句中使用来自包的自定义异常?,python,exception,error-handling,pygsheets,Python,Exception,Error Handling,Pygsheets,我有一个脚本,它使用pygsheets包将数据帧上传到Google sheets。如果数据帧为空,则会出现以下错误: InvalidArgumentValue: provide either cells or values, not both 因此,我一直试图在数据帧为空时使用try-except结构进行传递,但如果发生任何其他错误,则会引发异常。代码如下: try: pygsheets code except InvalidArgumentValue: pass except

我有一个脚本,它使用pygsheets包将数据帧上传到Google sheets。如果数据帧为空,则会出现以下错误:

InvalidArgumentValue: provide either cells or values, not both
因此,我一直试图在数据帧为空时使用try-except结构进行传递,但如果发生任何其他错误,则会引发异常。代码如下:

try:
    pygsheets code
except InvalidArgumentValue:
    pass
except:
    raise Exception
但是,当我尝试执行上面的行时,它会抛出以下错误:

NameError: name 'InvalidArgumentValue' is not defined

我该怎么解决呢?

假设您以通常的方式导入Pygsheet

import pygsheets
代码应该使用引用另一个包的成员类的常用语法引用该包中包含的异常,如

try:
    pygsheets code
except pygsheets.InvalidArgumentValue:
    pass
except:
    raise Exception