- 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