Classification and treatment of COMMON exceptions in Python

  • 2020-06-03 06:57:56
  • OfStack

Common exception types of Python are roughly divided into the following categories:

AssertionError: Exception thrown when the assert assertion condition is false

AttributeError: An exception that is thrown when the accessed object property does not exist

IndexError: Exception that is thrown when the object index is out of scope

4.KeyError: Look in the dictionary for an exception thrown by a nonexistent key

NameError: Exception thrown when accessing a variable that does not exist

OSError: An operating system exception

7.SyntaxError: This exception is thrown when syntax errors occur

8.TypeError: Type error, usually due to operations between different types

9.ZeroDivisionError: This exception occurs when the divisor is 0 while performing a mathematical operation

For more exceptions refer to the official documentation:

Link to version 2.7

3.6 Version link

Python exception handling:

Example 1: The simplest way to handle an exception that occurs


#!/usr/bin/python 
#coding:utf8 
 
#try with except Combined with the usage  
 
a = 1 
b = 2 
 
try : 
  assert a > b     # if a>b Thrown if judged false AssertionError abnormal  
except AssertionError:  # If you capture it AssertionError The exception will execute except The following code block  
  print ( "a<b" ) 

The output from the above example is a < b because a when asserted > When b is false, an AssertionError exception is thrown, and when caught, the statement in the except block is executed

Example 2: Using multiple except to catch exceptions


#!/usr/bin/python 
#coding:utf8 
 
#try With multiple except Combined with the usage , in try Blocks of code execute in sequence, stopping whenever an exception is caught  
 
a = 1 
b = 2 
c = "1" 
 
try : 
  assert a < b 
  d = a + c 
except AssertionError: 
  print ( "a<b" ) 
except TypeError,e:   # Here,  e  Is the exception information  
  print (e) 

The results of the above for unsupported operand type (s) for + : 'int and' str type does not support integer and string together, the assertion is true, won't appear so AssertionError exception, at that time after teaching, do the following statements is a TypeError exception, at this time will be executed except TypeError block of code below, behind e represent exception error message, so here is the result of the print out exception error information

Example 3: Use of try and except and else


#!/usr/bin/python 
#coding:utf8 
 
a = 1 
b = 2 
c = "1" 
 
try : 
  assert a < b 
  d = a + b 
except AssertionError,e: 
  print ( "a<b" ) 
except TypeError,e: 
  print (e) 
else :          # when try Execute the statement here when executing in a block of code finds no exception  
  print ( "Program execution successful" ) 

The above execution result is

Example 4: try and except combined with else and finally (without else)


#!/usr/bin/python 
#coding:utf8 
 
#try With multiple except Combined with the usage , in try Blocks of code execute in sequence, stopping whenever an exception is caught  
 
a = 1 
b = 2 
c = "1" 
 
try : 
  assert a < b 
  d = a + b 
  txt = open ( "/root/1.txt" ) 
  txt.write( "test" )    # Open file above by default r Mode opens, and this is going to be thrown IOError abnormal  
except AssertionError,e: 
  print ( "a<b" ) 
except TypeError,e:   # Here,  e  Is the exception information  
  print (e) 
except IOError,e: 
  print (e) 
else :          # Execute the statement here when no exception is found  
  print ( "Program execution successful" ) 
finally :         
# It is often carried out with or without the people finally A statement in a block of code, usually used to open a file and exit an exception during file processing when the file is not closed  
  txt.close() 


Related articles: