python基础教程学习笔记八

第八章 异常

Python中使用异常对象来表示异常情况

>>> 1/0

Traceback (most recent call last):

File"<pyshell#85>", line 1, in <module>

1/0

ZeroDivisionError: division by zero

自定义异常 类似于java中的throws

>>> raise Exception

Traceback (most recent call last):

File"<pyshell#86>", line 1, in <module>

raise Exception

Exception

>>> raise Exception('hyperdrive overload')

Traceback (most recent call last):

File"<pyshell#87>", line 1, in <module>

raiseException('hyperdrive overload')

Exception: hyperdrive overload

常见的异常类

BaseException
+--SystemExit
+--KeyboardInterrupt
+--GeneratorExit
+--Exception
+--StopIteration
+--ArithmeticError
|+--FloatingPointError
|+--OverflowError
|+--ZeroDivisionError
+--AssertionError
+--AttributeError
+--BufferError
+--EOFError
+--ImportError
+--LookupError
|+--IndexError
|+--KeyError
+--MemoryError
+--NameError
|+--UnboundLocalError
+--OSError
|+--BlockingIOError
|+--ChildProcessError
|+--ConnectionError
||+--BrokenPipeError
||+--ConnectionAbortedError
||+--ConnectionRefusedError
||+--ConnectionResetError
|+--FileExistsError
|+--FileNotFoundError
|+--InterruptedError
|+--IsADirectoryError
|+--NotADirectoryError
|+--PermissionError
|+--ProcessLookupError
|+--TimeoutError
+--ReferenceError
+--RuntimeError
|+--NotImplementedError
+--SyntaxError
|+--IndentationError
|+--TabError
+--SystemError
+--TypeError
+--ValueError
|+--UnicodeError
|+--UnicodeDecodeError
|+--UnicodeEncodeError
|+--UnicodeTranslateError
+--Warning
+--DeprecationWarning
+--PendingDeprecationWarning
+--RuntimeWarning
+--SyntaxWarning
+--UserWarning
+--FutureWarning
+--ImportWarning
+--UnicodeWarning
+--BytesWarning
+--ResourceWarning

自定义异常

>>> class SomeCustomException(Exception):pass

>>> raise SomeCustomException

Traceback (most recent call last):

File"<pyshell#8>", line 1, in <module>

raise SomeCustomException

SomeCustomException

捕捉异常

#未捕捉

x=input('enter the first number:')

y=input('enter the second number:')

z=eval(x)/eval(y)

print (z)

enter the first number:2

enter the second number:1

2.0

enter the first number:1

enter the second number:0

Traceback (most recent call last):

File"D:/workspace_python/exception.py", line 3, in <module>

z=eval(x)/eval(y)

ZeroDivisionError: division by zero

#捕捉

try:

x=input('enter the firstnumber:')

y=input('enter the secondnumber:')

z=eval(x)/eval(y)

print (z)

except ZeroDivisionError:

print('The second numbercan not be zero')

enter the first number:1

enter the second number:0

The second number can not be zero

多个except子句

try:

x=input('enter the firstnumber:')

y=input('enter the secondnumber:')

z=eval(x)/eval(y)

print (z)

except ZeroDivisionError:

print('The second number can not be zero')

except TypeError:

print('that was a number,was it?')

一个块捕捉多个异常

try:

x=input('enter the firstnumber:')

y=input('enter the secondnumber:')

z=eval(x)/eval(y)

print (z)

except (ZeroDivisionError,TypeError):

print('your numbers were bogus')

捕捉对象:

try:

x=input('enter the firstnumber:')

y=input('enter the secondnumber:')

z=eval(x)/eval(y)

print (z)

except ZeroDivisionError as e:

print('The second numbercan not be zero')

print(e)

enter the first number:1

enter the second number:0

The second number can not be zero

division by zero

全捕捉

try:

x=input('enter the firstnumber:')

y=input('enter the secondnumber:')

z=eval(x)/eval(y)

print (z)

except:

print('someThing wronghappended')

enter the first number:1

enter the second number:0

someThing wrong happended

Filally子句

try:

x=input('enter the firstnumber:')

y=input('enter the secondnumber:')

z=eval(x)/eval(y)

print (z)

except:

print('someThing wronghappended')

finally:

print('clenaing up...')

enter the first number:1

enter the second number:0

someThing wrong happended

clenaing up...

向上传播:

可以使用raise抛出异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值