Learning programming, for the beginning of the helpless beginners, the most afraid is not able to write code.

The fear is that a Python program will throw a horrible string and the whole program will stop.

And then it’s really hard to figure out how to solve this problem. Abnormal this ghost, not only let beginners horror, often used in the bird, the old bird is also very painful.

A fast running results of the program, due to abnormal and halfway out of the mood, how much blood?

Here’s how to prevent Python from going off the rails. Take a look below.

Python exception syntax

Are you often bothered by an error that causes an exception exit while Python is running?

Are you annoyed that you don’t know what went wrong?

Here’s a solution: Let Python’s exception syntax help.

Different Python versions have different exception mechanisms

Python2 and Python3 differ in Exception:

Examples of usage in Python 2.x:

1    try:
2         ......
3    except Exception,e:
4        raise e
Copy the code

Examples of usage in Python 3.x:

1      try:
2          ......
3      except Exception as e:
4           raise e
Copy the code

As you can see, in Python 3 it is as e.

Exception syntax Outline

Try to catch an exception (receive exception notification), return the program to a normal state, and continue execution.

Grammar description:

The AS clause is a variable used to bind the error object and can be omitted

There can be one or more except clauses, but there must be at least one

There can be at most one else clause and it can be omitted

There can be at most one finally clause and it can be omitted

Exception modes in actual scenarios

Common patterns of exceptions:

# -- -- -- -- -- -- -- -- -- -- -- -- -- a module: except the wrong type, capture the exception type -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # except ValueError: # except ZeroDivisionError: # print(' error occurred in internal statement of try, handled and returned to normal ') # print (' input number is 0, apple failed ') # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # - module 2: Except (error type 1, error type 2): execute this function if both error types are met ------ # except (ValueError, ZeroDivisionError): # print (' apple regardless of the ') # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - # - module 3: except: In addition to capture, no matter what exceptions are performing this function -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # except ValueError: # print(' a value error occurred inside a try statement, handled and returned to normal ') # except: # print (' received except ValueError error notification ') # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # - four modules: As variables: capture the error message with variable bindings -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - # # as the usage of the except ValueError as err: # print(' literal :', err) # invalid literal for int() with base 10: 'aaa' # print(' literal :', err) # invalid literal for int() with base 10: 'aaa' # Invalid literal for int() with base 10: 'aaa' # # exit procedures properly -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # - module: Else, only when there is no exception in the try to perform -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - # except ValueError: # print (' apple regardless of the ') # else: # print(' Inside the current try, no exceptions have occurred and the program is executing normally ') # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - # - six modules: ------------------ # finally except ValueError: print(' apple is not free ') except: Print (' ValueError ') else: print('else clause executed ') finally: print(' ValueError ') Print ('finally clause executed ') print(' program exits normally ')Copy the code

That’s all for today’s sharing. If you are interested in learning more about Python, please join our Python Learning Exchange Group.