From 19a43d9e1d63d1cf56a3a957f28ba85fcac8ae5e Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 11 Sep 2025 14:09:47 +0800 Subject: [PATCH] =?UTF-8?q?09-11-=E5=91=A8=E5=9B=9B=5F14-09-47?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01.基础语法/12.异常处理.md | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/01.基础语法/12.异常处理.md b/01.基础语法/12.异常处理.md index 6eb5b35..b96a7e8 100644 --- a/01.基础语法/12.异常处理.md +++ b/01.基础语法/12.异常处理.md @@ -10,12 +10,24 @@ #语法错误示范一 if + if + ^ +SyntaxError: invalid syntax + #语法错误示范二 def test: pass + def test: + ^ +SyntaxError: expected '(' + #语法错误示范三 -print(haha +print("hello" + + print("hello" + ^ +SyntaxError: '(' was never closed ``` ### 逻辑错误 @@ -25,9 +37,22 @@ print(haha num=input(">>: ") res1 = int(num) + ret = int(num) + ^^^^^^^^ +ValueError: invalid literal for int() with base 10: 'abc' + #无法完成计算 res1=1/0 + + a = 1/0 + ~^~ +ZeroDivisionError: division by zero + res2=1+'str' + + a = "hello" + 1 + ~~~~~~~~^~~ +TypeError: can only concatenate str (not "int") to str ``` ### 异常 @@ -45,6 +70,7 @@ res2=1+'str' # 触发IndexError l=['eagle','aa'] l[3] + # 触发KeyError dic={'name':'eagle'} dic['age'] @@ -56,8 +82,9 @@ int(s) 常见异常 -| AttributeError | 试图访问一个对象没有的属性,比如foo.x,但是foo没有属性x | +| 异常 | 描述 | | :---------------- | :----------------------------------------------------------- | +| AttributeError | 试图访问一个对象没有的属性,比如foo.x,但是foo没有属性x | | IOError | 输入/输出异常;基本上是无法打开文件 | | ImportError | 无法引入模块或包;基本上是路径问题或名称错误 | | IndentationError | 语法错误(的子类) ;代码没有正确对齐 | @@ -317,7 +344,7 @@ except EvaException as e: ### 断言 -表达式位True时,程序继续运行,表达式为False时程序终止运行,并报AssertionError错误 +表达式为True时,程序继续运行,表达式为False时程序终止运行,并报AssertionError错误 ```python assert 1 == 1