




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 10 * (1/0)Traceback (most recent call last): File , line 1, in ?ZeroDivisionError: division by zero 4 + spam*3Traceback (most recent call last): File , line 1, in ?NameError: name spam is not defined 2 + 2Traceback (most recent call last): File , line 1, in ?TypeError: Cant convert int object to st
2、r implicitlyl語法錯(cuò)誤和邏輯錯(cuò)誤不屬于異常,但有些語法錯(cuò)誤往往會(huì)導(dǎo)致異常,例如由于大小寫拼寫錯(cuò)誤而訪問不存在的對(duì)象。l當(dāng)Python檢測(cè)到一個(gè)錯(cuò)誤時(shí),解釋器就會(huì)指出當(dāng)前流已無法繼續(xù)執(zhí)行下去,這時(shí)候就出現(xiàn)了異常。異常是指因?yàn)槌绦虺鲥e(cuò)而在正??刂屏饕酝獠扇〉男袨椤異常分為兩個(gè)階段:第一個(gè)階段是引起異常發(fā)生的錯(cuò)誤;第二個(gè)階段是檢測(cè)并處理階段??梢岳^承Python內(nèi)置異常類來實(shí)現(xiàn)自定義的異常類。class ShortInputException(Exception):你定義的異常類。def _init_(self, length, atleast):Exception._init_(se
3、lf)self.length = lengthself.atleast = atleast try:s = raw_input(請(qǐng)輸入 - )if len(s) class MyError(Exception): def _init_(self, value): self.value = value def _str_(self): return repr(self.value) try: raise MyError(2*2) except MyError as e: print(My exception occurred, value:, e.value)My exception occur
4、red, value: 4 raise MyError(oops!)Traceback (most recent call last): File , line 1, in ?_main_.MyError: oops!try.except.結(jié)構(gòu)try子句中的代碼塊放置可能出現(xiàn)異常的語句,except子句中的代碼塊處理異常。try:try塊#被監(jiān)控的語句except Exception, reason:except塊#處理異常的語句 while True: try: x = int(input(Please enter a number: ) break except ValueError: p
5、rint(That was no valid number. Try again.)try.except.else.語句如果try范圍內(nèi)捕獲了異常,就執(zhí)行except塊;如果try范圍內(nèi)沒有捕獲異常,就執(zhí)行else塊。a_list = China, America, England, Franceprint 請(qǐng)輸入字符串的序號(hào)while True:n = input( )try:print a_listnexcept IndexError:print 列表元素的下標(biāo)越界,請(qǐng)重新輸入字符串的序號(hào)else: breakfor arg in sys.argv1: try: f = open(arg,
6、 r) except IOError: print(cannot open, arg) else: print(arg, has, len(f.readlines(), lines) f.close()import systry: f = open(myfile.txt) s = f.readline() i = int(s.strip()except OSError as err: print(OS error: 0.format(err)except ValueError: print(Could not convert data to an integer.)except: print(
7、Unexpected error:, sys.exc_info()0) raisel將要捕獲的異常寫在一個(gè)元組中,可以使用一個(gè)excep語句捕獲多個(gè)異常:import systry: f = open(myfile.txt) s = f.readline() i = int(s.strip()except (OSError, ValueError,RuntimeError, NameError): pass def divide(x, y): try: result = x / y except ZeroDivisionError: print(division by zero!) else:
8、 print(result is, result) finally: print(executing finally clause) divide(2, 1)result is 2.0executing finally clause divide(2, 0)division by zero!executing finally clause divide(2, 1)executing finally clauseTraceback (most recent call last): File , line 1, in ? File , line 3, in divideTypeError: uns
9、upported operand type(s) for /: str and strlPython2with open(d:test.txt) as f: for line in f:print linelPython3with open(myfile.txt) as f: for line in f: print(line, end=)import sys try: 1/0 except: tuple = sys.exc_info()print tuple完整/簡(jiǎn)寫命令用法示例解釋a(rgs) 顯示當(dāng)前函數(shù)中的參數(shù)b(reak) filename:lineno | function, condition b 173在173行設(shè)置斷點(diǎn)b function在function函數(shù)第一條可執(zhí)行語句位置設(shè)置斷點(diǎn)b不帶參數(shù)則列出所有斷點(diǎn),包括每個(gè)斷點(diǎn)的觸發(fā)次數(shù)、當(dāng)前忽略計(jì)數(shù)、以及與之關(guān)聯(lián)的條件b 175, condition設(shè)置條件斷點(diǎn),僅當(dāng)condition的值為True時(shí)該斷點(diǎn)有效cl(ear) filename:lineno | bpnumber bpnumber . cl清除所有斷點(diǎn)cl filename:lineno刪除指定文件指定行的所有斷點(diǎn)cl 3 5 9刪除第3、5、9個(gè)斷點(diǎn)condition bpnu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 行政管理心理學(xué)職能分析試題及答案
- 行政管理中的心理支持措施試題及答案
- 2025年中級(jí)經(jīng)濟(jì)師考試知識(shí)點(diǎn)試題及答案
- 工程項(xiàng)目評(píng)審試題及答案
- 2025年航空航天零部件制造高精度加工技術(shù)市場(chǎng)調(diào)研報(bào)告
- 2025年制造業(yè)智能制造標(biāo)準(zhǔn)體系報(bào)告:國(guó)家標(biāo)準(zhǔn)在制造業(yè)中的應(yīng)用與推廣
- 工程機(jī)械操作試題及答案
- 工程經(jīng)濟(jì)中的社會(huì)價(jià)值創(chuàng)造方法試題及答案
- 行政管理經(jīng)濟(jì)法應(yīng)用題試題及答案
- 2025年私域流量運(yùn)營(yíng)在零售行業(yè)中的客戶生命周期管理創(chuàng)新報(bào)告
- GA/T 1093-2023安全防范人臉識(shí)別應(yīng)用出入口控制人臉識(shí)別技術(shù)要求
- 港口危貨作業(yè)單位主要安全管理人員試題及答案(536道)
- 2024年監(jiān)理工程師考試《三控》真題與答案
- 2024年天津市初中地理學(xué)業(yè)考查試卷
- 《電工安全操作規(guī)范》課件
- 中考修改病句課件
- 體育裝備科技創(chuàng)新趨勢(shì)
- 宿舍樓施工安全管理體系與措施
- 預(yù)防未成年人犯罪的講座
- 臥床患者常見并發(fā)癥護(hù)理專家共識(shí)
- 管理干部安全生產(chǎn)培訓(xùn)課件
評(píng)論
0/150
提交評(píng)論