ParisgabrielStick to Handwriting day by day a decision hold on for a few years to dream for faith
Python ai from beginner to proficient
Add: The relative import of a package is only useful for the latter two import methods
Relative imports of packages are only useful for the latter two import methods
Exception (base) except:
What is an error:
An issue in which a logic or grammar-guided program fails to perform properly
What is an exception:
is a state of an identifier for a program error
When the exception is sent, the program will no longer execute down and go to the place where the function is called.
To process this error and revert to a normal state
The effect of an exception:
Used as a signal to notify upper-level callers of errors that need to be handled
Try statement:
Two kinds of syntax:
Ry-except statement (accept exception notification)
try-finally statements
Try-except Syntax:
Try
Statements that may trigger an exception
Except error type 1[as variable 1]:
Exception Handling Statements 2
Except error type 2[as variable 2]:
Exception Handling Statements 2
Except (Error type 3, error type 4, ...) ) [as Variable 3]:
Exception Handling Statements 3
......
Except
Exception handling Statement (other) (matches all types of errors)
Else
Exception statement does not occur (can be called without this statement not being made to an exception)
Finlly:
Final statement
Role:
Attempt to catch an exception, turn the idiom from an abnormal state to a normal state and execute normally
Example:
tar-except.py
Description
1. As clauses are variables used to bind the wrong object, you can omit
2. The EXCEPT clause can have one or more, but at least one
3. Else clause can have at most one or omit
4. The finally clause can have at most one, or omit to write
5. Else clause will be executed when the try does not appear an exception
6. The statement of the finally clause will be executed under any circumstances
try-finally Syntax:
Try
Statements that may trigger an exception
Finally
Final statement
Description
1.finally clauses can not be omitted
2. There must be no except clause
Role:
Usually try-finally statement to do things that must be handled when triggering an exception
The finally clause is bound to be executed regardless of whether the exception occurred
Attention:
The try-finally statement does not change the program's (normal/abnormal) state
Raise statement: (Initiate exception notification)
Role:
Triggers an error to allow the program to enter an abnormal state
Grammar:
Raise Exception type
Or
Raise Exception pairs
Instance:
Assert Assertion statement:
Grammar:
Assert truth expression, error data (usually a string)
Role:
When the true value expression is false, create a assertionerror with the wrong data
Type of error, and enter the exception state
Equivalent to:
if truth expression = = False
Raise Assertrtionerror (Error data)
Instance:
Summary:
Exception handling Statements (4):
Try-except statement:
Used to accept exception notifications, catch exceptions
Try-finally statement:
Execute the statement that must be executed
Raise statement:
Send exception notification and enter the exception state
Assert statement:
Send Assertionerror-type exception notifications based on conditional selection
Why to handle the exception mechanism:
When the number of program calls is deep, passing the error message to the keynote function requires a layer return
More trouble, with the exception handling mechanism can be simpler to pass the error message
All the error types in Python:
Python All error types
Type of error |
Description |
The following must be said: |
|
Zerodivisionerror |
Except (or modulo) 0 (all data types) |
ValueError |
Invalid parameter passed in |
Assertionerror |
Assertion statement failed |
Stopiteration |
There are no more values for iterators |
Indexerror |
This index is not in the sequence (index) |
Indentationerror |
Indentation Error |
OSError |
Input/output operation failed |
Importerror |
Failed to import module/object |
Nameerror |
Object not declared/initialized (no attributes) |
Attributeerror |
Object does not have this property |
|
|
Generatorexit |
Generator (generator) exception occurred to notify exit |
TypeError |
An operation that is not valid for type |
Keyboardinterrupt |
User interrupt execution (usually input ^c) |
Overflowerror |
Numeric operation exceeds maximum limit |
Floatingpointerror |
Floating-point calculation error |
Baseexception |
base class for all exceptions |
Systemexit |
Interpreter Request exited |
Exception |
base class for general errors |
StandardError |
Base class for all built-in standard exceptions |
Arithmeticerror |
base class for all numeric calculation errors |
Eoferror |
No built-in input, EOF Mark reached |
EnvironmentError |
Base class for operating system errors |
Windowserror |
System call failed |
Lookuperror |
base class for invalid data queries |
Keyerror |
This key is not in the map |
Memoryerror |
Memory overflow error (not fatal for Python interpreter) |
Unboundlocalerror |
To access uninitialized local variables |
Referenceerror |
Weak references (Weak reference) attempt to access objects that have been garbage collected |
RuntimeError |
General run-time errors |
Notimplementederror |
Methods that have not been implemented |
SyntaxError Python |
Syntax error |
Taberror |
Tab and Space Mix |
Systemerror |
General Interpreter system error |
Unicodeerror |
Unicode-related errors |
Unicodedecodeerror |
Error in Unicode decoding |
Unicodeencodeerror |
Unicode encoding Error |
Unicodetranslateerror |
Unicode Conversion Error |
The following are the warning types |
|
Warning |
Base class for warnings |
Deprecationwarning |
Warnings about deprecated features |
Futurewarning |
Warning about the change in the construction of future semantics |
Overflowwarning |
Old warning about auto-promotion to Long integer |
Pendingdeprecationwarning |
Warnings about attributes that will be discarded |
Runtimewarning |
Warning for suspicious run-time behavior (runtime behavior) |
Syntaxwarning |
Warning of suspicious syntax |
Userwarning |
Warnings generated by user code |
See: Help (builtins) |
Parisgabriel:python Full Stack engineer (0 basics to Mastery) Tutorial 21st (Package, module import)