'''
try:
<代码块1>被try关键字检查并保护的业务代码
except<异常的类型>:
<代码块2>代码块1出现错误后执行的代码块
'''
def upper (str_data):
new_str = str_data.upper()
return new_str
result = upper (1)
print (result)
>>AttributeError: 'int' object has no attribute 'upper'
对于上面的函数,如果不加任何处理的话,就会报错,添加了try代码块后,可以看到程序仍会继续运行
def upper (str_data):
new_str = 'None'
try:
new_str = str_data.upper()
except:
print ('程序出错了')
return new_str
result = upper (1)
print (result)
>>程序出错了
>>None
'''
try:
<代码块>
except Exception as e:
<异常代码块>
'''
def upper (str_data):
new_str = 'None'
try:
new_str = str_data.upper()
except Exception as e:
print ('程序出错了{}'.format(e))
return new_str
result = upper (1)
print (result)
>>程序出错了'int' object has no attribute 'upper'
>>None
'''
try:
<代码块>
except <具体异常类型> as e:
<异常代码块>
'''
def test ():
try:
1/0
print ('hello') #该代码不会执行
except ZeroDivisionError as e:
print (e)
test()
>>division by zero
当except代码块有多个的时候,当捕获到第一个后不会继续往下捕获
方法一:
def test ():
try:
print ('hello')
print (name)
except (ZeroDivisionError, NameError) as e:
print (e)
test()
>>name 'name' is not defined
方法二:
def test ():
try:
print ('hello')
print (name)
except ZeroDivisionError as e:
print (e)
except NameError as e:
print (e)
test()
>>name 'name' is not defined
异常集合1:
异常名称 | 说明 |
---|---|
Exception | 通用异常类型 |
ZeroDivisionError | 不能整除0 |
AttributeError | 对象没有这个属性 |
IOError | 输入输出操作失败 |
IndexError | 没有当前的索引 |
异常集合2:
异常名称 | 说明 |
---|---|
KeyError | 没有这个键 |
NameError | 没有这个变量 |
SyntaxError | 语法错误 |
SystemError | 解释器系统错误 |
ValueError | 传入的参数错误 |
无论是否发生异常,一定会执行的代码块
在函数中,即便在try或except中进行了return也会依然执行finally语法块
try语法至少要伴随exception或finally中的一个
'''
try:
<代码块1>
except:
<代码块2>
finally:
<代码块3>
'''
def test1():
try:
1/0
except Exception as e:
print (e)
finally:
return 'finally'
result = test1()
print (result)
>>division by zero
>>finally
def test1():
try:
1/0
except Exception as e:
print ('111')
return e
finally:
print ('finally')
result = test1()
print (result)
>>111
>>finally
>>division by zero
#使用的是finally里返回的值
def test1():
try:
1/0
except Exception as e:
print ('111')
return e
finally:
print ('222')
return 'finally'
result = test1()
print (result)
>>111
>>222
>>finally
#finally和try的组合使用
def test1():
try:
print ('111')
1/0
print ('222')
finally:
print ('333')
return 'finally'
result = test1()
print (result)
>>111
>>333
>>finally
# 自定义抛出异常函数 --raise
'''
raise 异常类型(message)
message:错误信息
'''
def test (number):
if number == 100:
raise ValueError ("number 不可以100")
def test2 (number):
try:
return test (number)
except ValueError as e:
return e
result = test2 (100)
print (result)
>>number 不可以100
def test4 (name):
if name == 'djk':
raise Exception ('djk nonono')
return name
test4('djk')
>>Exception: djk nonono
class NumberLimitError (Exception):
def __init__ (self, message):
self.message = message
class NameLimitError (Exception):
def __init__(self, message):
self.message = message
def test5 (name):
if name == 'djk':
raise NameLimitError('djk nonono')
return name
def test6 (number):
if number > 100:
raise NumberLimitError ("no 100")
return number
try:
test5('djk')
except NameLimitError as e:
print (e)
try:
test6(101)
except NumberLimitError as e:
print (e)
>>djk nonono
>>no 100
用于判断一个表达式,在表达式条件为false的时候触发异常
'''
assert expression,message
expression:表达式,一般是判断相等,或者判断是某种数据类型的bool判断的语句
message:具体的错误信息
'''
assert 1 == 1 #不会触发
assert 1 > 2,"1小于2"