异常处理

  • Exception:Python 内置的异常类
  • raise:抛出异常
  • try:尝试运行以下语句
  • except:在 try 语句之后,捕获某个异常,为空则捕获全部异常(很危险,难以 debug)
  • else:在 try 语句之后,如果没有捕获异常,则执行
  • finally:在 try 语句之后,无论是否捕获异常,均执行
try:
    print("Enter try.")
except:
    print("Enter except.")
else:
    print("Enter else.")
finally:
    print("Enter finally.")
Enter try.
Enter else.
Enter finally.
try:
    print("Enter try.")
    1 / 0
except:
    print("Enter except.")
else:
    print("Enter else.")
finally:
    print("Enter finally.")
Enter try.
Enter except.
Enter finally.
try:
    print("Enter try.")
    1 / 0
except ZeroDivisionError:
    print("Enter except ZeroDivisionError.")
except ArithmeticError:
    print("Enter except ArithmeticError.")
except:
    print("Enter except.")
else:
    print("Enter else.")
finally:
    print("Enter finally.")
Enter try.
Enter except ZeroDivisionError.
Enter finally.
MODE = "DEBUG"
try:
    print("Enter try.")
    1 / 0
except:
    print("Enter except.")
    if MODE == "DEBUG":
        raise
    else:
        print("Enter else.")
finally:
    print("Enter finally.")  # 先执行,再抛出异常
Enter try.
Enter except.
Enter finally.



---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

 in ()
      2 try:
      3     print("Enter try.")
----> 4     1 / 0
      5 except:
      6     print("Enter except.")


ZeroDivisionError: division by zero
MODE = "WARN"
try:
    print("Enter try.")
    1 / 0
except Exception as e:
    print("Enter except.")
    if MODE == "DEBUG":
        raise
    elif MODE == "WARN":
        print(e)
    else:
        print("Enter else.")
finally:
    print("Enter finally.")
Enter try.
Enter except.
division by zero
Enter finally.
MODE = "WARN"


def main():
    while True:
        try:
            print("Please input your Python 3 Expression.")
            exp = input()
            print("The result of your expression is", eval(exp))
        except Exception as e:
            if MODE == "DEBUG":
                raise
            elif MODE == "WARN":
                print(e)
            elif MODE == "STABLE":
                print("Something wrong, please input again.")
        else:
            break


if __name__ == "__main__":
    main()
Please input your Python 3 Expression.
1/0
division by zero
Please input your Python 3 Expression.
1=1
invalid syntax (, line 1)
Please input your Python 3 Expression.
2**4
The result of your expression is 16

异常的传播

def f1():
    try:
        return 1 / 0
    except Exception as e:
        print("This is f1.")
        print(e)


def f2():
    try:
        f1()
    except Exception as e:
        print("This is f2.")
        print(e)


def f3():
    try:
        f2()
    except Exception as e:
        print("This is f3.")
        print(e)


def main():
    try:
        f3()
    except Exception as e:
        print("This is main.")
        print(e)


if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print("This is __main__.")
        print(e)
This is f1.
division by zero
def f1():
    try:
        return 1 / 0
    except Exception as e:
        print("This is f1.")
        print(e)
        raise


def f2():
    try:
        f1()
    except Exception as e:
        print("This is f2.")
        print(e)


def f3():
    try:
        f2()
    except Exception as e:
        print("This is f3.")
        print(e)


def main():
    try:
        f3()
    except Exception as e:
        print("This is main.")
        print(e)


if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print("This is __main__.")
        print(e)
This is f1.
division by zero
This is f2.
division by zero
def f1():
    try:
        return 1 / 0
    except Exception as e:
        print("This is f1.")
        print(e)
        raise


def f2():
    try:
        f1()
    except Exception as e:
        print("This is f2.")
        print(e)
        raise


def f3():
    try:
        f2()
    except Exception as e:
        print("This is f3.")
        print(e)
        raise


def main():
    try:
        f3()
    except Exception as e:
        print("This is main.")
        print(e)
        raise


if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print("This is __main__.")
        print(e)
This is f1.
division by zero
This is f2.
division by zero
This is f3.
division by zero
This is main.
division by zero
This is __main__.
division by zero
def f1():
    try:
        return 1 / 0
    except Exception as e:
        print("This is f1.")
        print(e)
        raise


def f2():
    try:
        f1()
    except Exception as e:
        print("This is f2.")
        print(e)
        raise


def f3():
    try:
        f2()
    except Exception as e:
        print("This is f3.")
        print(e)
        raise


def main():
    try:
        f3()
    except Exception as e:
        print("This is main.")
        print(e)
        raise


if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print("This is __main__.")
        print(e)
        raise
This is f1.
division by zero
This is f2.
division by zero
This is f3.
division by zero
This is main.
division by zero
This is __main__.
division by zero



---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

 in ()
     37 if __name__ == "__main__":
     38     try:
---> 39         main()
     40     except Exception as e:
     41         print("This is __main__.")


 in main()
     28 def main():
     29     try:
---> 30         f3()
     31     except Exception as e:
     32         print("This is main.")


 in f3()
     19 def f3():
     20     try:
---> 21         f2()
     22     except Exception as e:
     23         print("This is f3.")


 in f2()
     10 def f2():
     11     try:
---> 12         f1()
     13     except Exception as e:
     14         print("This is f2.")


 in f1()
      1 def f1():
      2     try:
----> 3         return 1 / 0
      4     except Exception as e:
      5         print("This is f1.")


ZeroDivisionError: division by zero

你可能感兴趣的:(异常处理)