要求:用户输入半径求圆的面积,若半径为负值抛出异常
class Circle(Exception):
def __init__(self, R):
self.R = R
try:
r = int(input('请输入半径'))
if r <= 0:
raise Circle(r)
except Circle as error:
print('异常原因:半径不能为%d' % error.R)
else:
S = 3.14 * r ** 2
print('圆的面积为:', S)
运行结果: