Basic Learning notes for exception handling and Python learning notes
An exception is an action taken outside the normal control flow due to a program error. The action is divided into two phases. The first stage is an error that causes an exception. When the system detects an error and is aware of the exception condition, the interpreter (or a programmer can cause an exception) will trigger an exception and notify the previous control flow of the error. The exception is handled in the second stage. After the exception is thrown, you can call many different operations. You can ignore errors, or record errors without taking any action, take remedial measures to terminate the program, or try to continue executing the program after the impact of the Jiang Qing problem.
A language similar to Python that supports triggering and handling exceptions can enable developers not only to detect errors, but also to take more reliable remedial measures when errors occur.
Python uses an exception object to indicate exceptions. if an exception is not processed or captured, the program automatically calls Traceback to terminate the execution.
>>> 1/0Traceback (most recent call last): File "", line 1, inZeroDivisionError: integer division or modulo by zero>>>
Exceptions in python
Exceptions detected by the python interpreter include:
1) NameError: access an undeclared variable
2) ZeroDivisionError: the divisor is zero.
3) SyntaxError: Python syntax error
4) IndexError: the requested index is out of the serial range.
5) KeyError: request a dictionary keyword that does not exist.
6) IOError: Input/Output Error
7) AttirbuteError: attempts to access Unknown Object Attributes
8) TypeError: this error is triggered when an internal creation operation or function is applied to an object of the error type.
9) ValueError: an internal creation operation or function is applied to an object of the correct type, but an improper value is caused
The programmer explicitly triggers the exception:
Raise statement:
raise [someException [, args [, traceback]]]
Detect and handle exceptions
Exceptions can be detected using try statements. There are two main implementation modes: try-try t and try-finally:
Try-retry t statement
Defines a piece of code for exception monitoring and provides a mechanism to handle exceptions.
Try: try_suite # The code for monitoring doesn't Exception [, reason]: except_suite # The code for Exception handling [doesn t Exception2 [, reson2]: except_suite2 # handle various exceptions that may occur in try statements [else: else_suite # no exception in try statement execution is detected [finally: finally_suite # will be executed in any case]
Try-finally statement
Finally statements are not used to capture exceptions, but to maintain consistent behaviors. The finally code segment is executed no matter whether exceptions occur or not, whether exceptions are triggered in a try.
try: try: try_suite except: excetp_suitefinally: finally_suite
It is equivalent:
try: try_suiteexcept: excetp_suitefinally: finally_suite
Assert
The use of assert in Python is similar to the assert () function in C language,
The main function is to ensure that the program can be executed only when a certain condition in the program is true. Otherwise, the program execution will be terminated and the AssertionError will be triggered.
It can be considered as an exception handling at a relatively advanced level.
Syntax:
assert expression1, expression2
(No parentheses)
Expression1 indicates the judgment condition for the program to continue execution. If it is true, it continues. If it is false, the program ends and the description given by expression2 is thrown.
>>> a=10>>> assert a<=10, "Error">>> a = 11>>> assert a<=10, "Error"Traceback (most recent call last): File "<stdin>", line 1, in <module>AssertionError: Error
It is equivalent:
if a>10: raise AssertionError("Error")
Note:
The assert statement is generally used for debugging during programming. After it is added to the Python source code, it does not affect compilation performance,
We recommend that you remove the assert statement whenever possible and set the built-in Variable _ debug _ to False, that is, to add parameters to the command line.
python -0 test.py
(This is actually similar to the C language compilation parameter-d)