Python Exception and Debug


Python Exception and Debug

什么是异常

处理一些不可预料的情况,比如文件不存在,空间满了,通讯超时等等。

遇到这些问题了程序该如何执行?

这就是异常处理的价值和我们处理异常的目的。

Python常见的异常

  • ZeroDivisionError

  • NameError

  • TypeError

Eg:

ZeroDivisionError

image-20220618180916517

  • 这么写,没有任何的语法错误,IDE不会报错

image-20220618181014653

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33



```NameError

![image-20220618181230216](https://s2.loli.net/2022/06/18/Z1mzoOG9N7iL3aD.png)

- 之前没有定义过b,但是这里用到了



#### TypeError

![image-20220618181313133](https://s2.loli.net/2022/06/18/DK9Zi4vhFsSlWzN.png)

- 两个不同类型的数强制进行运算

## Python 的异常处理语法

```python
try:
程序执行语句块
except ExceptionName as alias:
   异常处理语句块
except ExceptionName as alias:
   异常处理语句块
except ExceptionName as alias:
   异常处理语句块
...
else:
   无异常时处理语句块
finally:
   必须处理语句块
类型有很多,可以层层分析,对应着处理。最后可以加上处理 Exception 的语句,因为 Exception是父类,能捕获所有的异常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

```{0}Exception

## 例子

### example 1 - 简单例子

```python
try:
   print(10/0)
except NameError as e:
   print(type(e))
   print(e)
except ZeroDivisionError as e:
   print(type(e))
   print(e)

print('-------------End of the process-------------')

结果

1
2
3
4
5
<class 'ZeroDivisionError'>
division by zero
-------------End of the process-------------

Process finished with exit code 0
  • 10/0 , ZeroDivisionError 的异常被成功 catch

example 2 - 合并两个 except

  • 从上面的例子可以看到,我们的程序对于两个不同异常的处理手段是一样的,那么可以合并
1
2
3
4
5
6
7
try:
   print(10/0)
except (NameError, ZeroDivisionError) as e:
   print(type(e))
   print(e)

print('-------------End of the process-------------')
1
2
3
4
5
<class 'ZeroDivisionError'>
division by zero
-------------End of the process-------------

Process finished with exit code 0
  • 依然成功执行了

example 3 - Exception

1
2
3
4
5
6
7
8
9
try:
   print('2'+2)
except (NameError, ZeroDivisionError) as e:
   print(type(e))
   print(e)
except Exception as e:
   print(type(e))
   print(e)
print('-------------End of the process-------------')
1
2
3
4
5
<class 'TypeError'>
can only concatenate str (not "int") to str
-------------End of the process-------------

Process finished with exit code 0
  • 我们可以在最后抓住Exception,这样不管任何类型的异常,无论考虑到的还是没考虑到的,都能抓住。
  • 我们没有针对TypeError 进行捕获,但依然catch了。

example 4 - else

1
2
3
4
5
6
7
8
9
10
11
try:
   a = 1+1
except (NameError, ZeroDivisionError) as e:
   print(type(e))
   print(e)
except Exception as e:
   print(type(e))
   print(e)
else:
   print('else')
print('-------------End of the process-------------')
1
2
3
4
else
-------------End of the process-------------

Process finished with exit code 0
  • 语句中没有任何错误,所以程序执行else语句
  • 当任一一个错误被捕获,else语句都不会被执行

example 5 - finally

1
2
3
4
5
6
7
8
9
10
11
12
13
try:
print(10/0)
except (NameError, ZeroDivisionError) as e:
print(type(e))
print(e)
except Exception as e:
print(type(e))
print(e)
else:
print('else')
finally:
print('finally')
print('-------------End of the process-------------')
1
2
3
4
5
6
<class 'ZeroDivisionError'>
division by zero
finally
-------------End of the process-------------

Process finished with exit code 0
  • finally 语句一定会执行
  • 就算程序没错误,执行了else,也会执行finally

Python 内置异常类型

image-20220618214423110



Author: Liang Junyi
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Liang Junyi !
  TOC