第八章 异常
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抛出异常