Example Analysis of Python catching exception
This article mainly explains the "Python capture exception example analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python capture exception example analysis" bar!
Exceptions in 1.python
Syntax error: the program is not written according to the grammar rules
Run error: run time error
Logic error: logic error
An exception object (exception object) is used to represent an exception
two。 Catch exception try-except
Exception with a divisor of 0:
Put the detected statement block into the try block and the exception handling statement block into the except block
If there is no exception in the detected statement block, the exception handling after except is ignored; otherwise, the exception handling statement block is executed.
Multiple except clauses
One except block catches multiple exceptions
Empty except: catches all exceptions
As statement:
Put the cause of the error in the err variable and print the variable
Else statement:
If there are no exceptions in the try block, the else statement is executed
If you want to type multiple times after an exception is generated until it is correct, you can add while True and break statements
The location of the break statement
Jump out of the loop if input is normal
Finally clause
Finally statements can be used in try statements, and the statement blocks in the finally clause are executed regardless of whether an exception occurs or not
Context Manager and with statements
The with statement assigns the value of the context management expression to the variable and executes its statement sequence
With automatically closes files after completion, which is the recommended usage for manipulating objects such as files and databases in python.
Thank you for your reading. The above is the content of "Python capture exception example Analysis". After the study of this article, I believe you have a deeper understanding of the problem of Python capture exception example analysis, and the specific use situation still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!