#!/usr/bin/env python3
# -*- coding: utf-8 -*-
try:
i = 1/0
except:
# 只写一个except会捕捉所有的异常
print("error")
try:
sum = 1 + '1'
f = open('aaaaa.txt')
print(f.read())
except (OSError, TypeError):
# 同时对多个异常的处理
print('出错了')
try:
i = 10 /2
except ZeroDivisionError as e:
print(e)
else:
# 如果没有发生异常,会继续执行else语句的
print('no error')
try:
i = 10 / 2
# 主动抛出一个异常
raise ZeroDivisionError
except:
print('捕获抛出异常')
网友评论