This article is a detailed list of Python common exception handling, for everyone to refer to, specifically as follows:
1. Throwing Exceptions and custom exceptions
Python uses an Exception object (Exception object) to represent an exception, and an exception is thrown when an error is encountered. If the exception object is not processed or captured, the program terminates execution with the so-called backtracking (Traceback, an error message).
①.raise statement
The Raise keyword in Python is used to throw an exception, which is basically the same as the throw keyword in C # and Java, as follows:
#--Coding:utf-8-
def thorwerr ():
Raise Exception ("Throw an Exception")
# Exception: Throw an exception
Thorwerr ()
After the Raise keyword is thrown as a generic exception type (Exception), in general the more detailed the exception is thrown, Python constructs a number of exception types within the exceptions module, using the Dir function to view the type of exception in the exceptions, As follows:
Import Exceptions
# [' Arithmeticerror ', ' assertionerror ' ...]
Print dir (exceptions)
Passing exceptions
Caught an exception, but wanted to throw it back (passing an exception), you can use a raise statement without arguments:
#--Coding:utf-8--
class Muffledcalculator:
muffled = False
def calc (self,expr):
try:
return eval (expr)
except Zerodivisionerror:
if self.muffled:
print ' Division by zero is illegal '
else:
Raise
②. Custom Exception type
Python can also customize its own special type of exception, only need to inherit from the exception class (directly or indirectly):
Class Somecustomexception (Exception): Pass
2. Catching exceptions
Similar to the Try/catch in C #, Python uses the Try/except keyword to catch exceptions as follows:
#--Coding:utf-8--
try:
print 2/0
except Zerodivisionerror:
print ' divisor cannot be 0 '
①. Catch multiple exceptions
When a except statement captures only the type of exception that is declared later, it is necessary to add a except statement if it is possible to throw another type of exception, or you can specify a more general type of exception, such as: Exception, as follows:
#--Coding:utf-8--
try:
print 2/' 0 '
except zerodivisionerror:
print ' divisor cannot be 0 '
except exception:< C6/>print ' Other type exception '
In order to catch multiple exceptions, in addition to declaring multiple except statements, you can list multiple exceptions as tuples after a except statement:
#--Coding:utf-8--
try:
print 2/' 0 '
except (zerodivisionerror,exception):
print ' An exception occurred '
②. Get exception information
Each exception will have some exception information, and in general we should record the exception information:
#--Coding:utf-8--
try:
print 2/' 0 '
except (zerodivisionerror,exception) as E:
# Unsupported operand type (s) for/: ' int ' and ' str '
print E
3. Finally clause
The finally clause and the TRY clause are used in combination, but unlike the except statement, finally the code within the FINALLY clause is executed regardless of whether an exception occurs inside the TRY clause. In general, finally, it is often used to close the file or in the socket.
#--Coding:utf-8--try:print 2/' 0 ' except (zerodivisionerror,exception): print ' occurs An exception ' finally:print ' executes '
regardless of whether an exception occurred