Python exception catch

1. Exception capture ( red font represents the format of each exception capture )

1. try..except ...
try:
code that is not correctly executed
except:
processing after an exception occurs
a. Try: the block statement below, if an exception is encountered, stop execution immediately, and then execute the block in except Statement
b. If no exception is thrown in try, the block statement below try will be executed, while the block statement in except will not be executed
c. It is not recommended to use this statement to catch exceptions, because it may ignore very serious exception information
2. try ... except ... except ...
try:
Unsure whether the code is executed correctly
except + Exception type:
catch specific exception
except:
catch all exceptions
except the exception type specified in the preceding a. Except + Exception type: catch the exception of the specified exception type
such as: except FileNotFoundError: catch the specified exception of this type
b. Except + exception type, you can add multiple exceptions, separated by commas, the added exception type is enclosed in brackets, Such as: except (exception type 1, exception type 2):
c. Except: catch all exceptions
except the exception type specified above d. Except + exception type and except module have only one The module will execute
3. try ... except ...
except Exception ...
try:
Not sure if the code is executed correctly
except + exception type:
catch a specific exception
except Exception:
catch all exceptions
except the exception type specified in the previous a. except Exception: catch exceptions in the form of classes, but also catch all exceptions
b. except Exception as e: The captured exception information is put in e.
4. try ... except ...
except Exception ...
else ...
try:
code not sure whether it is executed correctly
except + exception type:
catch specific exception
except Exception:
catch All exceptions except the exception type specified in the previous
else:
block statement
a. If the try block statement does not throw an exception, then the else block statement will execute
b. If the try block statement throws an exception, then it will execute except or except Exception in block statement, and the statement is not executed else block
5, the try ... the except ...
the except Exception ...
else ....
a finally .. .
the try:
Code uncertain whether correct
except + exception type :
Catch a specific exception
except Exception:
Catch all exceptions except the exception type specified in the previous
else:
block statement
finally:
block statement
a. Else and finally modules are optional, but if there is, it must be placed at the end
b. Finally is regardless of whether the try block statement is Exceptions will be executed.
6. Raise exceptions by yourself.
Try:
Code that is not correctly executed
except + exception type:
catch specific exception
raise + content
except Exception:
catch all exceptions except the exception type specified in the previous
else:
block Statement
finally:
block statement
a. After the try throws an exception, if the exception is caught by except + exception type module, you can throw additional exception content through the raise keyword

Guess you like

Origin www.cnblogs.com/liu-py-2019/p/12687460.html