Python 传递异常并在未返回异常时引发错误的方法
使用使用GoogleAPI的pygsheets库,我试图创建具有给定名称的电子表格,如果它还不存在的话 我收到异常pygsheets.exceptions.SpreadsheetNotFound: 因此,我需要一些类似于反向异常的东西,或者如果有更好的python实践,我们将非常感谢您的建议。try子句有一个else部分,如果没有引发类似名称的异常,但与众所周知的if else完全无关,则执行该部分。所以 try子句有一个else部分,如果没有引发类似名称但与众所周知的if-else完全无关的异常,则执行该部分。所以Python 传递异常并在未返回异常时引发错误的方法,python,pygsheets,Python,Pygsheets,使用使用GoogleAPI的pygsheets库,我试图创建具有给定名称的电子表格,如果它还不存在的话 我收到异常pygsheets.exceptions.SpreadsheetNotFound: 因此,我需要一些类似于反向异常的东西,或者如果有更好的python实践,我们将非常感谢您的建议。try子句有一个else部分,如果没有引发类似名称的异常,但与众所周知的if else完全无关,则执行该部分。所以 try子句有一个else部分,如果没有引发类似名称但与众所周知的if-else完全无关的异
def create_spreadsheet_with_api(connection, filename):
try:
connection.open(filename)
if (no exception):
raise exception file already exists
if (there exception):
connection.create(filename)
def create_spreadsheet_with_api(connection, filename):
try:
connection.open(filename)
except FileNotFoundError:
connection.create(filename)
else:
raise FileAlreadyExistsError