2022-05-18 14:30:20
浏览数 (1)
自定义异常与抛出异常
自定义抛出异常raise
用法
参数
返回值
自定义异常类
- 继承基类——
Exception
- 在
构造函数
中定义错误信息
代码
代码语言:javascript
复制# coding:utf-8
def test(number):
if number == 100:
raise ValueError('number 不可以是100')
return number
# result = test(100)
# print(result)
def test2(number):
try:
return test(number)
except ValueError as e:
return e
result = test2(100)
print(result)
def test3():
raise '错误了'
# test3()
def test4(name):
if name == 'dewei':
raise Exception('dewei不可以被填写')
return name
# test4('dewei')
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 == 'dewei':
raise NameLimitError('dewei不可以被填写')
return name
def test6(number):
if number > 100:
raise NumberLimitError('数字不可以大于100')
return number
print('----------')
try:
test5('dewei')
except NameLimitError as e:
print(e)
try:
test6(1000)
except NumberLimitError as e:
print(e)
# test5('dewei')