Python随心记--异常处理

异常处理
try:
    pass
    
except ValueError as e:
    print(e)
异常处理:多分支
try:
    pass
    
except ValueError as e:
    print(e)
    
except KeyError as e:
    print(e)
万能异常
try:
    pass

except Exception as e:
    print(e)
else:   #try 中的代码没有异常的时候会执行elsel里面的代码
    pass
finally:   #不管有没有异常都会执行
    pass 
自定义异常
class EgonException(BaseException):
    def __init__(self,x):
        self.x = x

print(EgonException('自定义异常'))  
断言
  如果不正确 则报异常
assert 1 == 2
if 1 != 2:
    raise AttributeError

猜你喜欢

转载自www.cnblogs.com/Essaycode/p/10224120.html