Python series - exception handling

Keywords: Python

python provides exception handling try....except in the following format

  1. try:  
  2.     print("Logical processing code block")  
  3.   
  4. except Exception as e:  
  5.     print("Code block execution error exception", e)  
  6.   
  7. else:  
  8.     print("Logical code block execution no error execute this part")  
  9.   
  10. finally:  
  11.     print("Code block part that executes with or without errors")  

As shown above, python calls the Exception class when it catches the Exception. This is an Exception base class, and e is an Exception object similar to

e = Exception()

But we know that the instantiated e should be an object. If print(e) is executed, it should be a memory address. For example:

  1. class A():  
  2.     pass  
  3.   
  4. a = A()  
  5. print(a)  
  6. Result:  
  7. <__main__.A object at 0x00000000031ACE48>  

So why did we return an error message when we called print(e).

Let's look at the following example:

  1. class A():  
  2.     def __init__(self):  
  3.         pass  
  4.   
  5.     def __str__(self):  
  6.         return "class A str function"  
  7.   
  8. a = A()  
  9. print(a)  
  10.   
  11. Result:  
  12. class A str function  

Yes, after adding a str function to the class, when print(a) is no longer the memory address, it automatically calls the str function. So print(e) also calls the Exception's STR function.


raise anomaly

In the process of executing code with try...exception statement, the system will return error information to variable e after exception. See the following example:

  1. a = 1  
  2. b = 2  
  3. try:  
  4.     c = a + b  
  5.     if c == 4:  
  6.         print(c)  
  7.     else:  
  8.         raise Exception("Error")  
  9. except Exception as e:  
  10.     print(e)  
  11. Result:  
  12. Error  

Yes, we can return the error information to e through the exception class through raise, so that we can output our customized error information. Is it convenient to see. In this case, we can customize a class


Custom exception

Here's an example:

  1. class MyException(Exception):  
  2.     CODE1 = "10000"  #Wrong user name and password  
  3.     CODE2 = "10001"  #User does not exist  
  4.   
  5.     def __init__(self, error_msg):  
  6.         self._msg = error_msg  
  7.   
  8.     def __str__(self):  
  9.         if self._msg == "10000":  
  10.             return "Wrong user name and password"  
  11.         if self._msg == "10001":  
  12.             return "user does not exist"  
  13.   
  14. if __name__ == "__main__":  
  15.     user = input("User name:")  
  16.     pwd = input("Password:")  
  17.     try:  
  18.         if user == "peter":  
  19.             if pwd == "12345":  
  20.                 print("Login successfully")  
  21.             else:  
  22.                 raise MyException("10000")  
  23.         else:  
  24.             raise MyException("10001")  
  25.     except MyException as e:  
  26.         print(e)  

Posted by j7n5u on Sun, 05 Apr 2020 00:57:41 -0700