1 回答

TA貢獻1752條經(jīng)驗 獲得超4個贊
語法應(yīng)該是這樣的:
try:
#this expression was not defined anywhere, hence the NameError
print(leromipsum)
except NameError as error1:
print("meh")
因此,在您的代碼中,我認為您應(yīng)該先刪除標識:
except:requests.RequestException as error1:
print ("error1:",error1)
并在 except 后刪除冒號:
except requests.RequestException as error1:
print ("error1:",error1)
干杯
編輯:
try:
print("this code runs")
raise requests.HTTPError
print("this code is skipped, if an error occurs")
except requests.RequestException as error1:
print ("error1:",error1)
except requests.exceptions.HTTPError as error2:
print ("error1:",error2)
except requests.exceptions.ConnectionError as error3:
print ("error3:",error3)
except requests.exceptions.Timeout as error4:
print ("error4:",error4)
這個最小的例子對我有用。
添加回答
舉報