day9: exception handling (try, except, else, finally)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43264177/article/details/102763531

Fourth, exception handling
First, what is abnormal?
What is an exception: when an error is detected, can not continue, there have been some wrong prompt when the program is running, this is abnormal
1. The computer program common abnormal
Here Insert Picture DescriptionHere Insert Picture Description
2. We write the code abnormal
(1) variable is not defined
Here Insert Picture Description
(2) syntax error
Here Insert Picture Description
(3) key does not exist
Here Insert Picture Description
(4) did not find module
Here Insert Picture Description
(5) the type of error
Here Insert Picture Description
3. exception occurred and what the consequences will lead to
what is abnormal? When the program runs when an error is detected, can not continue, there have been some false tips, this is the exception.
Second, the exception analysis
1. Introduction abnormal type of
Here Insert Picture Description
detailed exception type address: Add a link description
2. Analysis of Abnormal
How traced abnormal (1) problems encountered, the error?
(2) abnormal cases
Here Insert Picture Description
should continue to do 3. how abnormal procedure?
Third, the exception caught
1. abnormality-live the except the try:
(. 1) the try:
The following is a possible occurrence of abnormal codes
(2) except NameError:
The following is captured after the exception processing program.
** Note points: ** do not write back except exception type, default catch all types of exceptions
(1) if we want to capture anomalies print out the information, how should we do this time?
except ... as e:
added as abnormality information can be extracted after the except stored in the variable.
Here Insert Picture Description
2. capturing a plurality of types of exception specified
way a:
(. 1) the try:
may occur exception code.
(2 except (NameError, ValueError)
to capture two or more exception handling scheme.
Second way:
(. 1) the try:
abnormal code is possible.
(2) the except a ValueError
captured TabError exception processing scheme.
(. 3) the except NameError:
captured NameError exception handling program.
three ways:
so if you are unsure of the type of capture, we can use the Exception.
Here Insert Picture Description
3. exception handling else and finally
issues review: statement learned that can be used separately else What does it mean? ?
judgment statement
loop statement
(1) try-except-else
exception is caught inside the statement used in any place else?
Here Insert Picture Description
this else, is not captured in the case of an anomaly, will run, very simple.
2.try-the except- a finally-the else
a finally effect?
a finally following code, is whether or not to get captured exceptions, will run the code.
Here Insert Picture Description

# @time:2019/10/27 7:34
# @Author:coco
# @File:test_try.py
# @software:PyCharm

"""
异常捕获
try:
except:
else:
finally:

"""

a=100

try:
    print(a+'abc')
except:
    print('捕获到了异常')

try:
    with open('ttt.txt','r') as f:
        f.read()
except:
    # 捕获到了异常
    print('打开的文件不存在')
    with open('ttt.txt','w') as f:
        pass

operation result:
Here Insert Picture Description
Here Insert Picture Description

# @time:2019/10/27 7:56
# @Author:coco
# @File:02指定捕获异常的处理.py
# @software:PyCharm


"""
异常捕获
try:
except:
else:
finally:

"""

a = 100

try:
    print(a + 'abc')
# except后面可以写异常类型(指定只捕获这类型的错误)
except TypeError as e:
    print('捕获到了异常')
    print(e)
except NameError:
    print('捕获变量没有被定义的错误')

print('python666')

# 方式二:一个except指定捕获多异常类型
try:
    print(a + 'abc')
# except后面可以写异常类型(指定只捕获这类型的错误)
except (TypeError, NameError) as e:
    print('捕获到了类型异常')
    print(e)

# 捕获所有的异常类型(语法错误除外)

try:
    # aa = 10000
    print(a + 'abc')
# except后面可以写异常类型(指定只捕获这类型的错误)
except Exception as e:
    print('捕获到了类型异常')
    print(e)
else:
    # try里面没有捕获异常
    print('没有捕获到异常')
finally:
    # rty里面的代码不管是否发生异常,它都会执行
    print('----------finally---------')

Guess you like

Origin blog.csdn.net/weixin_43264177/article/details/102763531