raise Exception('test')
import exceptions
dir(exceptions) #列出python中所有种类的异常
class CustomException(Exception): #自定义异常继承自Exception
pass
try:
x = input('1: ')
y = input('2: ')
print(x/y)
except ZeroDivisionError:
print('1234')
try:
x = input('1: ')
y = input('2: ')
print(x/y)
except ZeroDivisionError:
raise
except TypeError:
raise
except (ZeroDivisionError, TypeError, NameError):
raise
except (ZeroDivisionError, TypeError, NameError) as e: #捕捉异常并在console中输出
print(e)
except: #捕捉全部异常,但需要谨慎使用,有时候有些错误会被忽略
print('error')
except Exception as e: #用这个except捕捉全部异常好一些
print(e)
try:
print('123')
except:
print('456')
else:
print('bye')
Exception