Error
Exception
1 raise 语句显式的抛出异常
2 Python 解释器自己检测到异常并引发它
3 程序会在异常抛出的地方中断执行,如果不捕获,就会提前结束程序(其实是终止当前线程的执行)
def foo():
print('before')
print(1/0) # 捕获异常
print('after')
foo()
# ZeroDivisionError: division by zero
def foo():
print('before')
raise Exception('My Exception') # raise 主动抛出异常
print('after')
foo()
# Exception: My Exception
try:
待捕获异常的代码块
except [异常类型]:
异常的处理代码块
此例执行到c = 1/0
时产生异常并抛出,由于使用了try...except
语句块则捕捉到了这个异常
异常生成位置之后语句块将不再执行,转而执行对应的except
部分的语句
最后执行try...except
语句块之外的语句
def foo():
try:
print('before')
c = 1/0
print('after')
except:
print('error')
print('catch the exception')
foo()
print('====== end ======')
before
error
catch the exception
====== end ======
def foo():
try:
print('before')
print(1/0)
print('after')
except ArithmeticError: # 指定捕获的类型
print('error')
print('catch the exception')
foo()
print('====== end ======')
before
error
catch the exception
====== end ======
Exception hierarchy
The class hierarchy for built-in exceptions is:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StopAsyncIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
| +-- ModuleNotFoundError
+-- 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
| +-- RecursionError
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- EncodingWarning
+-- ResourceWarning
BaseException
及子类BaseException
:所有内建异常类的基类是 BaseException
SystemExit
:sys.exit()
函数引发的异常,异常不捕获处理,就直接交给Python
解释器,解释器退出KeyboardInterrupt
:对应的捕获用户中断行为 Ctrl + C
Exception
及子类:Exception
是所有内建的、非系统退出的异常的基类,自定义异常类应该继承自它SyntaxError
:Python
将这种错误也归到异常类下面的Exception
下的子类,但是这种错误是不可捕获的ArithmeticError
:所有算术计算引发的异常,其子类有除零异常等LookupError
:使用映射的键或序列的索引无效时引发的异常的基类:IndexError
, KeyError
从Exception
继承的类sys.exit()
示例 1import sys
KeyboardInterrupt
print('Before')
sys.exit(1)
print('SysExit')
print('outer')
D:\PycharmProjects\pythonProject\202108089\venv\Scripts\python.exe D:/PycharmProjects/pythonProject/202108089/test6.py
Before
Process finished with exit code 1
sys.exit()
示例 2import sys
try:
sys.exit(1)
except SystemExit:
print('SysExit')
print('outer')
D:\PycharmProjects\pythonProject\202108089\venv\Scripts\python.exe D:/PycharmProjects/pythonProject/202108089/test6.py
SysExit
outer
Process finished with exit code 0
KeyboardInterrupt
示例try:
import time
while True:
time.sleep(1)
print('!!!')
except KeyboardInterrupt:
print('Ctrl + C')
print('=== end ===')
PS D:\PycharmProjects\pythonProject\202108089> python .\test8.py
!!!
!!!
Ctrl + C
=== end ===
PS D:\PycharmProjects\pythonProject\202108089>
SyntaxError
示例try:
0a = 5
except SyntaxError: # 无法捕获
print('after')
SyntaxError: invalid syntax
print('=' * 10)
def 0a():
print('begin')
try:
0a()
except Exception as e:
print(e, type(e))
D:\PycharmProjects\pythonProject\202108089\venv\Scripts\python.exe D:/PycharmProjects/pythonProject/202108089/test8.py
File "D:/PycharmProjects/pythonProject/202108089/test8.py", line 1
def 0a():
^
SyntaxError: invalid syntax
Process finished with exit code 1
class MyException(Exception):
pass
try:
raise MyException
except MyException: # 捕捉自定义异常
print('Catch the exception')
Out:
Catch the exception
多种捕获:except可以指定捕获的类型,捕获多种异常。
import sys
class MyException(Exception):
pass
try:
print('before')
a = 1/0
# raise MyException()
# open('t')
# sys.exit(1)
except MyException:
print('catch my exception')
except ArithmeticError: # 可以捕获除零异常
print('ari')
except ZeroDivisionError: # 可以捕获除零异常
print('zero')
except MyException:
print('catch my exception')
except BaseException: # 可以捕获除零异常
print('Base')
except Exception: # 可以捕获除零异常
print('exception')
except:
print('sysexit')
class A:pass
try:
raise A()
except:
print('catch the exception')
Out:
catch the exception
as
子句:被抛出的异常,应该时异常类的实例,可以使用as
子句获得这个对象raise
语句:raise
后要求应该是BaseException
类的子类或实例,如果是类,将被无参实例化;raise
后什么都没有,表示抛出最近一个被激活的异常,如果没有被激活的异常,则抛出类型异常;raise
是用raise
语句来引发一个异常。异常/错误
对象必须有一个名字,且它们应是Error
或Exception
类的子类Python
用异常对象(exception object)
表示异常情况,遇到错误后,会引发异常Traceback
,一种错误信息)终止执行finally
子句:最终,即最后一定要执行的,try...finally
语句块中,不管是否发生了异常,都要执行finally
的部分else
子句:没有任何异常发生,则执行class MyException(Exception):
def __init__(self, code, message):
self.code = code
self.message = message
try:
raise MyException()
raise 1/0
# raise
except MyException as e:
print('catch my exception')
except Exception as e:
print(e)
print('=== end ===')
Out:
__init__() missing 2 required positional arguments: 'code' and 'message'
=== end ===
class MyException(Exception):
def __init__(self, code, message):
self.code = code
self.message = message
try:
# raise MyException()
raise 1/0
# raise
except MyException as e:
print('catch my exception')
except Exception as e:
print(e)
print('=== end ===')
Out:
division by zero
=== end ===
f = None
try:
f = open('111.txt')
except Exception as e:
print(e.__class__, e.errno, e.strerror)
finally:
print('clear working')
try:
f.close()
except Exception as e:
print(e)
Out:
<class 'FileNotFoundError'> 2 No such file or directory
clear working
'NoneType' object has no attribute 'close'
# 函数的返回值取决于最后一个执行的return语句,而finally则是try...finally中最后执行的语句块
def foo():
# return 1
try:
return 3
finally:
return 5
print('Finally')
print('===')
print(foo())
Out:
5
try:
ret = 1/0
# ret = 1 * 0
except ArithmeticError as e:
print(e)
else:
print('OK')
finally:
print('fin')
division by zero
fin
try:
# ret = 1/0
ret = 1 * 0
except ArithmeticError as e:
print(e)
else:
print('OK')
finally:
print('fin')
OK
fin
foo2
调用了foo1
,foo1
产生的异常,传递到了foo2
中
异常总是向外层抛出,如果外层没有处理这个异常,就会继续向外抛出
如果内层捕获并处理了异常,外部就不能捕获到了
如果到了最外层还是没有被处理,就会中断异常所在的线程的执行
注意整个程序结束的状态返回值
def foo1():
return 1/0
def foo2():
print('foo2 start')
foo1()
print('foo2 stop')
foo2()
D:\PycharmProjects\pythonProject\202108089\venv\Scripts\python.exe D:/PycharmProjects/pythonProject/202108089/test6.py
Traceback (most recent call last):
... ...
return 1/0
ZeroDivisionError: division by zero
foo2 start
Process finished with exit code 1
# 线程中测试异常
import threading
import time
def foo1():
return 1/0
def foo2():
time.sleep(3)
print('foo2 start')
foo1()
print('foo2 stop')
t = threading.Thread(target=foo2)
t.start()
while True:
time.sleep(1)
print('Everything is OK.')
print(threading.enumerate())
try
语句try
语法try:
<语句> # 运行别的代码
except <异常类>:
<语句> # 捕获某种类型的异常
except <异常类> as <变量名>:
<语句> # 捕获某种类型的异常并获得对象
else:
<语句> # 如果没有异常发生
finally:
<语句> # 退出try时总会执行
try
工作原理# try嵌套
# 内部捕获不到异常,会向外层传递异常
# 但是如果函数内层有finally且其中有return break语句,则异常就不会继续向外抛出
try:
try:
ret = 1/0
except KeyError as e:
# except ZeroDivisionError as e:
print(e)
finally:
print('inner fin')
except:
print('outer catch')
finally:
print('outer fin')
inner fin
outer catch
outer fin
# try嵌套
# 内部捕获不到异常,会向外层传递异常
# 但是如果函数内层有finally且其中有return break语句,则异常就不会继续向外抛出
def foo():
try:
ret = 1/0
except KeyError as e:
print(e)
finally:
print('inner fin')
return
try:
foo()
except:
print('outer catch')
finally:
print('outer fin')
inner fin
outer fin
1 立即捕获:需要立即返回一个明确的结果
2 边界捕获:封装产生了边界
def parse_int(s):
try:
return int(s)
except:
return 0
print(parse_int('s'))
Out:
0
# 自己写的异常类没写对,抛出异常,被exception捕获
class MyException(Exception):
def __init__(self, code, msg):
self.code = code
self.msg = msg
try:
raise MyException # 抛出的异常都是实例
except NotImplementedError as e:
print('not imp', e, type(e))
except MyException as e:
print('my exception', e, type(e))
except Exception as e:
print('all exception', e, type(e))
print('end')
all exception __init__() missing 2 required positional arguments: 'code' and 'msg' <class 'TypeError'>
end
class MyException(Exception):
def __init__(self, code, msg):
self.code = code
self.msg = msg
try:
raise MyException(999, '123')
except NotImplementedError as e:
print('not imp', e, type(e))
except MyException as e:
print('my exception', e, type(e))
except Exception as e:
print('all exception', e, type(e))
print('end')
my exception (999, '123') <class '__main__.MyException'>
end
class MyException(Exception):
def __init__(self, code="200", msg="Not OK"):
self.code = code
self.msg = msg
try:
raise MyException # 等效于raise MyException()
except NotImplementedError as e:
print('not imp', e, type(e))
except MyException as e:
print('my exception', e, type(e), e.code)
except Exception as e:
print('all exception', e, type(e))
print('end')
my exception <class '__main__.MyException'> 200
end