异常
在Python中,程序在执行过程过产生的错误称为异常,如:列表索引越界、打开不存在的文件等。
在Python中,所有异常类都是Exception的子类。每次执行程序遇到错误的时候,程序就会引发异常。如果这个异常对象没有进行处理和捕捉,程序就会用所谓回溯(trace back,一种错误信息)终止执行,这些信息包括错误名称、原因和错误发生的行号。
常见异常
异常名称 | 原因 |
NameError | 尝试访问一个未声明的变量 |
ZeroDivisionError | 当除数为零的时候 |
SyntaxError | 语法错误 |
IndexError | 序列下标错误 |
KeyError | 字典中不存在的键访问时 |
FileNotFoundError | 尝试打开不存在的文件时 |
AttributeError | 当尝试访问未知的对象属性时 |
异常处理
1、try-except
利用try-except捕获处理异常
例如:
def div(a,b):
return a/b
if __name__=="__main__":
try:
a,b=5,3
print(div(a,b))
# c,d=5,0
# print(div(c,d))
e,f="hello","python"
print(div(e,f))
except ZeroDivisionError:
print("除数为零,请重新输入")
except TypeError:
print("类型错误,请重新输入")
except Exception:
print("其他问题,请重新输入")
输出结果:
1.6666666666666667
类型错误,请重新输入
一条语句捕获多个异常、else、finally
例如:
def div(a,b):
return a/b
if __name__=="__main__":
try:
a,b=5,3
print(div(a,b))
c,d=5,0
print(div(c,d))
# e,f="hello","python"
# print(div(e,f))
# except Exception:
# print("其他问题,请重新输入")
except (ZeroDivisionError,TypeError) as e:
print("出现错误,请重新输入")
print(e)
except Exception:
print("其他问题,请重新输入")
else:
print("异常不发生时,会执行的代码,发生时,不执行的代码段.....,通常类似于goto.....")
finally:
print("总会被执行的代码段,通常用于资源回收,但python不太常用")
输出结果:
1.6666666666666667
出现错误,请重新输入
division by zero
总会被执行的代码段,通常用于资源回收,但python不太常用
2、抛出异常
例如:
try:
print("hello....")
raise NameError
# name=NameError()
# raise name
# index=IndexError()
# raise index
raise IndexError("hello index......")
except NameError:
print("产生了异常....")
try:
raise IndexError
# raise IndexError from Exception
except IndexError:
print("在NameError中,跑出了IndexError....")
except IndexError as e:
print("index......")
print(e)
try:
raise
except IndexError:
print("我们再一次抛出的异常类")
输出结果:
hello....
产生了异常....
在NameError中,跑出了IndexError....
3、assert语句
assert语句又称作断言,指的是期望用户满足指定的条件。当用户定义的约束条件不满足的时候,它会触发AssertionError异常,所以assert语句可以当作条件格式的raise语句。
例如:
i=1
print("--------")
try:
assert i==10,"i应该等于10"
except AssertionError as e:
print("assetion error....")
print(e)
print("hello.....")
输出结果:
--------
assetion error....
i应该等于10
hello.....
模块
with资源管理器
class myObj:
def __enter__(self):
print("myobj enter.......")
return self
def __exit__(self,a,b,c):
print("资源回收的代码.....")
def a(self):
print("my function......")
if __name__=="__main__":
with myObj() as obj:
print("with block......")
raise Exception
obj.a()
输出结果:
myobj enter.......
with block......
资源回收的代码.....
Traceback (most recent call last):
File "h:/大二/Python/source/模块/6-5-7.py", line 24, in <module>
raise Exception
Exception
包、模块的导入
模块的导入:
import math
print(math.e)
print(math.pi)
print(math.pow(2,5))
from math import sin
print(math.sin(math.pi/2))
print(sin(math.pi/2))
from math import pi
print(sin(pi/2))
输出结果:
2.718281828459045
3.141592653589793
32.0
1.0
1.0
1.0
包的导入:
test源码:
def add(a,b):
return a+b
def sub(a,b):
return a-b
if __name__=="__main__":
print("hello python")
print(add(7,0))
print("test ...over")
test1源码:
import test
print(test.add(3,5))
print(test.sub(3,5))
from test import add,sub
print(add(1,2))
print(sub(3,4))
print(__name__)
输出结果:
8
-2
3
-1
__main__