1. Issues in the proceedings
1.1 Low-level grammatical errors
Low-level errors are purely syntax errors, and the code mainly reports errors at the writing and debugging stage.
>>> if True SyntaxError: invalid syntax >>>
The above error is a very easy syntax error to make at first, with the colon (:) missing after True.
1.2 Intermediary errors: hidden errors in the code
Invisible errors are mainly logical errors or flaws in code writing that report errors or give incorrect answers when the program meets specific data processing conditions.
>>> def print_D(dic): i=0 len1=len(dic) while i < len1: print(()) # Randomly delete and return an element i+=1 >>> print_D({1:'a',2:'b'}) (2, 'b') (1, 'a')
The above code works fine, but in actual program use, perhaps after another programmer passes a non-dictionary object.
>>> print_D([1,2,3,4]) Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> print_D([1,2,3,4]) File "<pyshell#9>", line 5, in print_D print(()) AttributeError: 'list' object has no attribute 'popitem'
An incorrectly passed object that causes a normal program to go wrong is an implicit error. Implicit errors are characterized by the program running normally under normal circumstances, and errors under special circumstances (e.g., boundary values are not taken into account, the type of incoming data is not double-checked, etc.). Some implicit errors are not even reported, but are output by the result, which is the worst case.
1.3 Advanced Errors: Unusual Software Errors in the Face of Uncertainty
High-level errors refer to uncertainty errors, which mainly refer to software where the code itself is not faulty and the data entered can be controlled or guaranteed, but rather uncertainty anomalies brought about by the environment during operation. Such as:
(1) The software itself to try to open a file, and the file has been destroyed or exclusive (exclusive refers to a process (thread) processing, other processes (threads) can not be synchronized processing, you can only wait for the process (thread) processing is complete, other processes (threads) can continue to operate).
(2) Sudden network interruption during the process of inserting data into the database by the software, resulting in data loss.
(3) Failure of the software operating hardware, resulting in the software not functioning properly, etc.
(4) The database system is corrupted and the software reports errors when reading or writing data.
(5) Software inputs are excessively complex or incorrectly operated.
2. Capturing exceptions
2.1 Basic Exception Catching Statements
Basic Exception Catching Statement Syntax
try: code module1 except: code module2
The try keyword, which indicates the beginning of the catch exception statement; code module 1, which belongs to the code module that needs to be executed normally; and the except keyword, which is used to catch the exception information and can give the error message (default English prompt).
(1) The try statement is executed first, representing the start of the exception catching mechanism.
(2) Execute code module 1, if there is no error, ignore the except keyword and code module 2, the code is executed normally.
(3) If an exception occurs during the execution of code module 1, terminate the execution of the remaining code in code module 1 and go to except.
(4) except keyword to catch the exception information, and the execution of the code module 2 (often give an error message or make a mistake after the problem handling), the end of the exception handling
typical example
>>> def print_D(dic): i=0 try: len1=len(dic) while i < len1: print(()) i+=1 except: print('Error passing value type, must be dictionary type!') >>> print_D({1:'a',2:'b'}) (2, 'b') (1, 'a') >>> print_D([1,2,3,4]) Error passing value type,Must be a dictionary type! # Correct message after an error
2.2 Exception Handling with a Finally Clause
Basic format:
try: code module1 except: code module2 finally: code module3
After FINALLY keywording code module 3, run the code function that executes code module 1 whether or not there is an error.
Case 1
>>> try: 1/0 except: print('The divisor cannot be zero') finally: print('End of execution occurs') >>>
Case 2
>>> try: 1/2 except: print('Can't be 0 for book publishing') finally: print('End of run appears') >>>
#Running results
0.5
End of run appears
Case 3
import sys try: 1/0 except: print('The divisor cannot be zero') () finally: print('End of program execution') print('Can I execute the code?') >>>
#Running results
The divisor cannot be 0
End of program execution
2.3 The try-except-else structure
Structure of the running process: the program into the try statement part, when the try statement part of the exception occurs, then into the except statement part, if no exception occurs into the else statement part.
try: print("Normal code.") except Exception as e: print("will not be exporting this sentence.") else: print("This sentence will be exported.") print('-'*30) try: print(1/0) except Exception as e: print("Go to Exception Handling.") else: print("Will not be exported.") >>>
#Running results
normal code
This sentence will be output
------------------------------
Go to Exception Handling
2.4 try-except-else-finally framework
try: print("No anomalies.") except Exception as e: print("It won't output!") else: print("Go to else") finally: print("Must output!") print('-'*30) try: print(1/0) except Exception as e: print("Causes anomalies.") else: print("Won't go to else.") finally: print("Must output!") >>>
# Running results
There are no anomalies.
Go to else
Must be output!
------------------------------
cause anomalies
Must be output!
2.5 Capturing Specific Exception Information
3. Throwing exceptions
python allows programmers to trigger exceptions themselves, which can be done with the raise keyword, using the format:
raise [Exception]
The Exception parameter is optional and its object is a subclass of the Exception class as shown in the table above.
Example 1: Trigger without parameters
>>> raise Traceback (most recent call last): File "<pyshell#52>", line 1, in <module> raise RuntimeError: No active exception to reraise >>>
The code throws a no-questions-asked exception message via the raise clause.
Example 2: Trigger with parameters
>>> i='1' >>> if type(i)!=int: raise TypeError('Error in type i!') Traceback (most recent call last): File "<pyshell#57>", line 2, in <module> raise TypeError('Error in type i!') TypeError: iType error! >>>
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!