第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python異常處理錯誤RequestException

python異常處理錯誤RequestException

千萬里不及你 2023-12-12 20:41:38
我正在 python 應(yīng)用程序中嘗試 try catch 方法。我不確定我犯了什么錯誤,是縮進問題還是其他問題這是我的代碼。try:            ivurl = f'{api}/company/{id}/contacts?'            payload: Dict[str, Union[bool, int]] = {'id': True}            response = get_iv_response(url=ivurl, payload=payload)            email_contact_totalresult: int = response.get('totalResults')            email = int(email_contact_totalresult)            payload: Dict[str, Union[bool, int]] = {'name': True}            response = get_iv_response(url=ivurl, payload=payload)            phone_contact_totalresult: int = response.get('totalResults')            phone = int(phone_contact_totalresult)            print ("here")            if email > 0 or phone > 0:                print (f"{id} has contacts")                update_query = f"""update section                                 set has_contact = true ,                                updated = '{cur_date}'                                  where name = '{id}'                            """                print(update_query)                insert_query = f"""insert into quota(name,age)                                values ('{name}',                                        '{age}')"""                print (insert_query)                cursor.execute(update_query)                cursor.execute(insert_query)                            conn.commit()            else :                print (f"{id} doesnot  has contacts")        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)    print ("job done")    print(end_time)    print('Duration: {}'.format(end_time - start_time))我在“as”無效語法附近遇到錯誤本期圖片錯誤: https: //ibb.co/Wn5pnM3
查看完整描述

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)

這個最小的例子對我有用。


查看完整回答
反對 回復(fù) 2023-12-12
  • 1 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號