In general, an exception occurs when a program is unable to handle normal logical execution. In order to handle exceptions and errors during a program’s execution, Python also defines a number of standard exceptions and exception handling mechanisms for handling exceptions that occur during the program’s execution.

In Python, the main keywords for handling exceptions are: try, except, else, finally, and the raise keyword.

Try keyword: mainly detects the exception, if the exception occurs, the program will be handled to except keyword.

Except keyword: Get the exception and handle it.

Else: After executing the block in the try field, execute the contents of the else block if no exception is found.

Finally keyword: Enter the domain of this keyword for processing, whether or not an exception occurs. Usually, it mainly deals with necessary operations such as resource closure, object memory release, etc.

Raise keyword: Used to throw custom exception messages so that the program cannot execute directly down.

Exception keywords are usually combined in a variety of ways, and different combinations can be used in different exception handling scenarios. Reasonable exception handling can not only improve the logical operation in the process of program execution, but also improve the performance of program execution.

try… except… Else combination exception handling

1 try: 2 print "except Exception",e: 4 print "except Exception",e: 5 else: 6 print "except Exception",e: 6 print "except Exception",e: 6 print "except Exception"
2 try: 2 print: 1 try: 2 print: 2 except: 4 print: 2 else: 6 print: 2 try: 2 print: 1 try: 2 print: 2 except: 4 print: 2 else: 6 print: 2 try: 2 print: 1 try: 2 print: 2 except: 4 print: 2

In this combination, when dealing with exceptions, the first processing method follows the except keyword with a specific exception object, while the second one does not. If no exception object is added in accordance with the second case, it means that all exceptions are captured and processed. The disadvantage is that specific exception information cannot be output.

1 try: 2 print "except windowsError ",e1:4 print" except windowsError ",e1. Message 5 print "except windowsError ", e2: 7 print "",e2. Message 8 print" "

This is the combination of multiple exception handling, by connecting the form of multiple except keywords to catch different exceptions and exception handling program logic.

try… Finally combines exception handling

3 finally: 4 print 1 try: 2 print 3 finally: 4 print

Executes normal program logic. Executes the program logic in the finally block whether or not an exception is detected in the try block.

try… except… Finally combines exception handling

1 try: 2 try: 3 try: 4 print: 5 finally: 6 print: 1 try: 2 print: 5 finally: 6 print: 7 except Exception,e: 8 print "exception: ",e.message 9 print" exception:"

Many programming languages also provide operations that allow you to customize exceptions, and Python has its own ways to customize exceptions and use them. The same way of thinking is used, which is to inherit standard exceptions, encapsulate them into their own exception objects, and then throw custom exceptions in the appropriate processing logic.

Define custom exceptions

1class CustomException(Exception):
2    def __init__(self,err):
3        self.args = err

Use custom exceptions

1 a = int(raw_input()) 2 if a < 0: 3 raise customException (" ") 4 else: 5 print"

More exciting to WeChat official account [Python concentration camp], focus on back-end programming practice, original articles updated every day!