L7-2 Exception Handling

First, in classroom teaching

We learned the lesson text processing file operations, know how to open, close the file, we will learn this lesson dealing operation.

Second, exception handling

When we write programs, sometimes you step on a lot of pit long as we are writing bug and change the way the bug. To reduce the bug generation, the lesson we have to learn to start a repair operation after manufacturing bug.

1. What is abnormal

An exception is the error signal generation program is running (when the program error occurs, an exception, if the program does not handle it, it will throw the exception, run the program also will be terminated), in python, false triggering the following abnormal

 

 

 

2. The types of abnormalities

# AttributeError: there is no attempt to access an object, such as foo.x, but did not attribute foo x 
# IndexError: try to access the index value is out of range of series such as x ,, only three elements, is trying to access x [5] 
# KeyError : trying to access does not exist in the dictionary of key 
# NameError: try to access the variable does not exist 
# SyntaxError: Python code is illegal, the code does not compile (no requirement to write syntactically) 
# TypeError: incoming object type does not comply with the requirements of ( for different data types operate error) 
# ZeroDivisionError: divisor can not be 0, in addition to being given 0 
# FileNotFoundError: using a file, the file can not be found

3. Handling Exceptions

There are three main ways:

 3. 1 If an error condition is unpredictable, you need to use try ... except: processing after an error

  try-except statement error detection try block, so except statement catching and handling

# Basic syntax of 
the try : 
    code block to be detected 
except exception types: 
    the try in the event of an anomaly is detected, on the implementation of this position logic 
# example 

the try : 
  DIC = { " name " : " Rita " , " Age " :. 17 }
  del dic [ " Sex " ]
 the except KeyError:
  Print ( " you want to delete key does not exist " )

3.2 try-finally statement, whether an exception occurs will perform the final code

# Basic syntax of 
the try : 
    Code blocks are detected 
the finally :
     the try abnormality detecting 

the try : 
    FH = Open ( " homework.txt " , " W " ) 
    fh.write ( " my job finished " ) 
    SUM = +. 1 " 1 " 
a finally : 
    fh.close () 
    Print ( " Erro: file not found or read the file failed " )

3.3 try-except-else if there is an exception, execution except statement, if not exceptional execution else statement

the try : 
    f = Open ( " homework.txt " , " w " , encoding = " UTF-8 " ) 
    f.write ( " My work can be considered finished " )
 the except FileNotFoundError:
     Print ( " Error: file not found or read take file failed " )
 the else :
     Print ( " content successfully written to the file " ) 
    f.close ()

III. Practice

Simple division calculator

Requirements: a program to be able to handle invalid input, for example, when the input divisor is 0, the prompt divisor can not be zero.

Print ( " Enter two numbers, let him compare the door: " )
 the while True: 
    FIRST_NUM = int (the INPUT ( " Please enter the first number: " ))
     IF FIRST_NUM == " q " :
         BREAK 
    second_num = int ( iNPUT ( " Please enter the second number: " ))
     the try : 
        answer = int (FIRST_NUM) / int (second_num)
     the except the ZeroDivisionError:
         Print ( " divisor is not 0 " )
     the else :
         Print(answer)

IV Summary

# 1. Under normal circumstances, an exception occurs when the Python program can not be processed normally. 
# 2. When an exception occurs we need to capture Python script to handle it, otherwise the program will be terminated. 
# 3. Python exception is handled by try-except statement, try-finally statement, else statement.

Guess you like

Origin www.cnblogs.com/xiaoxiao-ya/p/12093667.html