---restore content starts---
Python Exception Handling
Python provides two very important features to handle the exceptions and errors that Python programs run in. You can use this feature to debug a python program.
- Exception handling: This site Python tutorial will be described in detail.
- Assertion (Assertions): This site Python tutorial will be described in detail.
Python Standard exception
Baseexception base class for all exceptions
Systemexit Interpreter Request Exit
Keyboardinterrupt user interrupt execution (usually input ^c)
Exception base class for general errors
Stopiteration iterator with no more values
。。。。。。。
。。。。。。。
Python Exception Handling
Python provides two very important features to handle the exceptions and errors that Python programs run in. You can use this feature to debug a python program.
- Exception handling: This site Python tutorial will be described in detail.
- Assertion (Assertions): This site Python tutorial will be described in detail.
Python Standard exception
Baseexception base class for all exceptions
Systemexit Interpreter Request Exit
Keyboardinterrupt user interrupt execution (usually input ^c)
Exception base class for general errors
Stopiteration iterator with no more values
。。。。。。。
。。。。。。。
What is an exception?
An exception is an event that occurs during program execution and affects the normal execution of the program.
In general, an exception occurs when Python does not handle the program properly.
The exception is a Python object that represents an error.
When a Python script exception occurs, we need to capture and process it, or the program terminates execution.
Exception handling
You can use the Try/except statement to catch an exception.
The try/except statement is used to detect errors in a try statement block, allowing the except statement to catch exception information and handle it.
If you do not want to end your program when an exception occurs, simply capture it in a try.
Grammar:
The following is a simple syntax for try....except...else :
Try :< statements > # Run other code except < name >:< statement > # if the ' name ' exception is thrown in the try section except < name >,< data >:< statement > # if the ' name ' exception is thrown, get additional data Else :< statement > # If no exception occurs
Try works by starting a try statement, and Python is tagged in the context of the current program so that when an exception occurs, it can go back here, the TRY clause executes first, and what happens next depends on whether an exception occurs at execution time.
- If an exception occurs when the statement after the try is executed, Python jumps back to the try and executes the first except clause that matches the exception, and the control flow passes through the entire try statement (unless a new exception is thrown when the exception is handled).
- If an exception occurs in the statement after the try, but there is no matching except clause, the exception will be submitted to the upper try, or to the top of the program (This will end the program and print the default error message).
- If no exception occurs when the TRY clause executes, Python executes the statement after the Else statement (if there is else), and then the control flow passes through the entire try statement.
Use except without any exception type
You can use except without any exception type, as in the following example:
Try : normal operation ......... ........ except : An exception occurred, execution of this code ... ...... ..... Else : If there is no exception to execute this piece of code
The try-except statement above captures all occurrences of the exception. But this is not a good way to identify specific exception information through the program. Because it catches all the exceptions.
try-finally statements
The try-finally statement executes the final code regardless of whether an exception occurs.
Try :< statement >finally:< statement > # always executes raise when exiting a try
parameter of the exception
An exception can take a parameter that can be used as the output exception information parameter.
You can use the except statement to catch the parameters of the exception, as follows:
Try : normal operation ......... ........ except Exceptiontype, Argument: You can output the value of Argument in this ...
。。。。。
There is an excerpt from the Rookie tutorial
Python Dafa Good--exception