Python study notes (32-33) anomaly and exception handling (attached: python standard exceptions summary)

Learning Topics: Exception Handling
learning Date: 2020-02-09
Python Version: 3.7.4

People can not escape from doing wrong.
An exception is wrong, abnormal operation may be a programmer or user of the operating results lead to errors.

We can not force the user through the professional point of view and habits to use.
Therefore, we consider the program error caused due to misuse, in order to improve the applicability of the program.

32 speak python standard exceptions summary
Here Insert Picture Description
Here Insert Picture Description

The full version, please click https://fishc.com.cn/thread-45814-1-1.html

A few examples of it

  • NameError
>>> a
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    a
NameError: name 'a' is not defined
#这里提示是NameError,查看上表原因是,
#尝试访问一个不存在的变量
  • SyntaxError
>>> for ii in range(2)
SyntaxError: invalid syntax
#这里提示是SyntaxError,查看上表原因是语法错误
#仔细查代码,原因是for最后没有加冒号‘:’。

Lecture 33 how to handle exceptions

file=open('alexma.txt','r')
file.read()
file.close()

Here Insert Picture Description
An error, because this file simply do not exist.
So prompt FileNotFoundError.

How to avoid it, to use try except statement

try:
    file=open('alexma.txt','r')
    file.read()
    file.close()
except OSError:
    print('文件出错!')

Here Insert Picture Description
And then refine it:

try:
    file=open('alexma.txt','r')
    file.read()
    file.close()
except OSError as ErrorReason:
    print('文件出错!,原因是:'+str(ErrorReason))

Here Insert Picture Description
Even wow, this also print out the reasons why the error, so great! !

Various types of errors it, how to add it.

try:
    a=1+b
    file=open('alexma.txt','r')
    file.read()
    file.close()
except OSError as ErrorReason:
    print('文件出错!,原因是:'+str(ErrorReason))
except NameError as ErrorReason:
     print('命名错误,,原因是:'+str(ErrorReason))

Here Insert Picture Description
try except statement once the error, then the program will not go down to the next executed.

How to avoid it, you can also use try finally statement

try finally usage

try:
    检测范围
except Exception  [as ErrorReason]:
    出现异常后的处理代码,这里一般是提醒报错的代码
finally :
     无论如何都会被执行的代码

For instance, right

try:
    file=open(r'C:\Users\SNIPER\Desktop\mydemo、alexma.txt','w')
    file.write('I am alexma')
    b=b+1
    file.close()
except OSError as ErrorReason:
    print('文件出错!,原因是:'+str(ErrorReason))
except NameError as ErrorReason:
     print('命名错误,,原因是:'+str(ErrorReason))

If not finally, runs as follows:
Here Insert Picture Description
Here Insert Picture Description
The results above two screenshots, this is a reminder not defined named b, the program terminates, so file.close () This line of code is not executed,
do not perform this step, the content will not save file, the file is empty.

This file is empty problem often occurs in the actual programming, plus finally on ok

try:
    file=open(r'C:\Users\SNIPER\Desktop\mydemo\alexma.txt','w')
    file.write('I am alexma')
    b=b+1  
except OSError as ErrorReason:
    print('文件出错!,原因是:'+str(ErrorReason))
except NameError as ErrorReason:
     print('命名错误,,原因是:'+str(ErrorReason))
finally:
     file.close()

Here Insert Picture Description
Here Insert Picture Description
This time the content is saved to a text file.

raise statement

>>> a=1
>>> b=0
>>> a/b
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a/b
ZeroDivisionError: division by zero
>>> raise ZeroDivisionError('除数不能为0')
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    raise ZeroDivisionError('错啦,记住除数不能为0')
ZeroDivisionError: 除数不能为0
>>> 

Here Insert Picture Description

Published 79 original articles · won praise 55 · views 8964

Guess you like

Origin blog.csdn.net/hahahahhahha/article/details/104237473