Abbreviated python errors and anomalies

python errors and exceptions
  1, assert (assertion) is used to determine an expression, triggering an exception is false in expression. assert direct return conditions are not met in the case of running error, without waiting for the situation after the collapse of running, for example, our code can only run on Linux systems, you can determine whether the current system conditions.
  The syntax: assert expression is equivalent to:
      IF expression The Not:
        The raise AssertionError with
  2, except parser syntax error is found, the operation of error may also, at this time is called exception: As ZeroDivisionError, NameError and the like TypeError Wait.
  Exception handling:
    . 1) try / except: Exception Capture:
    Architecture:
      try: {# A try statement may contain more than one except clause, to deal with different specific exceptions. At most one branch will be executed.
          Code execution. (If there is in the process of execution of the try clause exception, then the rest of the try clause is ignored.)
      }
      The except: when the type of exception {# meet the following code is executed, an exception if there is no match with any excep then the exception will be passed to the top of the try.
            Code execution when an exception occurs. (When no exception occurs, this part is ignored)
      }
    Note: a plurality of abnormality except clause can handle simultaneously, these exceptions will be placed in a parenthesis as a tuple, for example:
      except (RuntimeError, TypeError, NameError):
        Pass
    addition, last except clause may ignore the name of the exception, it will be treated as a wildcard.

    2) try / except ... else: try / except statement has an optional else clause, if you use this clause, must be placed after all except clauses.
    Architecture:
      try: {# A try statement may contain more than one except clause, to deal with different specific exceptions. At most one branch will be executed.
          Code execution. (If there is in the process of execution of the try clause exception, then the rest of the try clause is ignored.)
      }
      The except: when the type of exception {# meet the following code is executed, an exception if there is no match with any excep then the exception will be passed to the top of the try.
            Code execution when an exception occurs. (When no exception occurs, this part is ignored)
      }
      the else: {
          code that executes when no abnormality
      }
    . 3) the finally the try-: the finally statement regardless of whether the try-exception will perform final code generation.
    Architecture:
      try: {# A try statement may contain more than one except clause, to deal with different specific exceptions. At most one branch will be executed.
          Code execution. (If there is in the process of execution of the try clause exception, then the rest of the try clause is ignored.)
      }
      The except: when the type of exception {# meet the following code is executed, an exception if there is no match with any excep then the exception will be passed to the top of the try.
        Code execution when an exception occurs. (When no exception occurs, this section will be ignored)
      }
      the else: {
          code that executes when no abnormality
      }
      a finally: {
            with or without abnormal code will be executed
      }
    4) throws an exception: raise: Python using the raise statement throws a specified exception.
    Syntax: The raise [Exception [, args [, the traceback]]]
      EG:
        X = 10
        IF X>. 5:
        The raise Exception ( 'X is not greater than the value 5.x: {}'. Format (x ))
      or more execution code hair triggers an exception:
        Traceback (MOST recent Results Last Call):
         File "test.py", Line 3, in <Module>
          raise Exception ( 'x is not greater than the value 5.x: {}' the format (X).)
        Exception: X is not greater than 5. x values are: 10

3) predefined clean-up behavior: Keywords with statement can be guaranteed, such as object files and the like will carry out his right cleaning method after use:
EG:
  with Open ( "myfile.txt") AS f:
    for in f Line:
      Print (Line, End = "")
  or more after the implementation of the code, even if in the process a problem, file f is always closed.

Guess you like

Origin www.cnblogs.com/yangrongkuan/p/12080811.html