异常处理

异常处理

一丶什么异常

1.语法错误

2.逻辑错误

二丶Exception

s1 = 'hello'
try:
    int(s1)
except Exception as e:
    print(e)

二丶try...except总结

  1. 把错误处理和真正的工作分开来

  2. 代码更易组织,更清晰,复杂的工作任务更容易实现;

  3. 毫无疑问,更安全了,不至于由于一些小的疏忽而使程序意外崩溃了;

三丶抛出异常raise

try:
    raise TypeError('抛出异常,类型错误')
except Exception as e:
    print(e)

四丶断言

assert 1 == 1
try:
    assert 1 == 2
except Exception as e:
    print(e)
原文地址:https://www.cnblogs.com/shiqizz/p/11514973.html