• RuntimeError
def ilove(name):
    if name=='陈培昌':
        print('i love {0}'.format(name))
    elif name == '程劲':
        print('i love {0}'.format(name))
    else:
        try:
            raise RuntimeError('who is he')
        except Exception as e:
            return e.args[0]
    return "这就是我的男神"
print(ilove('xxx'))

输出结果:

who is he

print(ilove('程劲')

输出结果:

i love 程劲
这就是我的男神

如果不把runtimeerror放入try catch中,则程序会因报错而中断
  • 如果这样写,则执行结果会很不优雅
def ilove(name):
    if name=='陈培昌':
        print('i love {0}'.format(name))
    elif name == '程劲':
        print('i love {0}'.format(name))
    else:
        try:
            raise RuntimeError('who is he')
        except Exception as e:
            return e.args[0]
    print("这就是我的男神")

运行:print(ilove('程劲'))

输出结果:

i love 程劲
这就是我的男神
None

 

相关文章:

  • 2021-12-18
  • 2021-08-11
  • 2021-09-02
  • 2021-10-05
  • 2021-04-04
  • 2021-11-30
  • 2022-02-20
  • 2021-05-04
猜你喜欢
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
相关资源
相似解决方案