python - Abnormal

Python with the exception object (exception object) to indicate anomalies. After an error is encountered, an exception is thrown. If the exception object has not been processed or captured, the program will use the so-called back (traceback) to terminate execution.

Most errors cause an exception, but does not necessarily represent an exception error, sometimes just a warning, sometimes just a termination signal, such as loop exits, etc.

Common exception handling syntax is as follows:

  • try...  except....
  • try...  except...  else...
  • try...  except...  finally...
  • raise (the initiative to throw an exception)
  • Common exceptions

The following were introduced:

1. try...  except....

 The possible error code in try, the exception processing code in the except

Open a file does not exist, catch this exception, the word line is printed out 
. 1
the try : 2 F = Open ( ' nothing.txt ' , ' RT ' ) . 3 the except FileNotFoundError: . 4 Print ( ' anomaly ' )

 All exception classes inherit from the Exception class, Exception class inherits from BaseException class (base class). All we can also use the Exception or BaseException to receive all types of exceptions

Base class may also be used to catch exceptions 
. 1
the try : 2 F = Open ( ' nothing.txt ' , ' RT ' ) . 3 the except Exception: # or BaseException . 4 Print ( ' anomaly ' )

These are abnormal when printing text, but how to accurately know the line that is a problem, use as msg

Print detailed cause of the abnormality 
. 1
the try : 2 print (AA) . 3 F = Open ( ' nothing.txt ' , ' RT ' ) . 4 the except Exception AS msg: # define variable msg, for receiving abnormality information, and print by the following Print 5 Print (msg) # prints: name 'aa' is not defined

 

2. try...  except...  else...

 No abnormal code when placed in else

. 1  the try :
 2      my_string = INPUT ( ' Enter several characters ' )
 . 3  the except EOFError:        # Ctrl + D, throw this exception 
. 4      Print ( ' input is forcibly interrupted ' )
 . 5  the else :
 . 6      Print ( ' no abnormality! this is the character you just entered: ' , my_string)

 

3. try...  except...  finally...

Regardless of whether there is an exception code that will be executed, finally put in

For example: closed, lock release file, the return connection to the database connection pool operations like 

. 1  the try :
 2      F = Open ( ' named abc.txt ' , ' RT ' )      # normally open an existing file 
. 3      Print (AA)
 . 4  the except Exception AS MSG:
 . 5      Print (MSG)                     # catch exceptions printing: name ' AA 'IS not defined 
6  a finally :
 7      f.close ()
 8      Print ( ' close the file ' )               # regardless of whether there is an exception, will be executed, close the file, and print: close the file

 

4. raise (the initiative to throw an exception)

 Be thrown by the raise statement

. 1  class ShortInputException (Exception):      # 1. custom exception class that inherits from Exception 
2      DEF  the __init__ (Self, length, atleat):
 . 3          self.legth = length
 . 4          self.atleast = atleat
 . 5  
. 6  the try :
 . 7      text = INPUT ( ' Please enter some characters: ' )
 . 8      IF len (text) <. 3 :
 . 9          the raise ShortInputException (len (text),. 3)     # 2. thrown 
10  the except EOFError:
 . 11      Print ( ' being terminated manually' )
 12 is  the except ShortInputException AS EX:    # 3. reception anomaly 
13 is      Print ( ' Expect AT Least {}, {} But your INPUT IS ' .format (ex.atleast, ex.legth)) # 4. Use of the exception object atleast and legth fields to print an error message

 

5. Common exceptions

Base class BaseException new all exception classes of 
the base class Exception all exception classes, but inherited from BaseException class
AssertionError assert statement fails
FileNotFoundError trying to open a nonexistent file or directory
AttributeError tries to access an object has no properties
OSError system errors, such as disks Manchu and other
NameError use a variable has not been assigned
IndexError when a sequence is beyond the scope
SyntaxError syntax error
KeyboardInterrupt Ctrl + c
TypeError incoming object type inconsistent with the request

 

Guess you like

Origin www.cnblogs.com/xiaochongc/p/12343203.html