zoukankan      html  css  js  c++  java
  • Python3-笔记-D-001-异常与断言

    import sys
    # === 捕获异常 (可灵活组合) ===
    def excep():
    # - try except -
    try:
    print(ex)
    except: # 捕获所有异常
    print("捕获异常!")

    try:
    print(ex)
    except: # 通过函数获取异常信息
    types, value, back = sys.exc_info() # 捕获异常
    sys.excepthook(types, value, back) # 打印异常

    try:
    print(ex)
    except NameError: # 捕获指定异常
    print("捕获名字未定义异常!")
    except IOError:
    print("捕获IO异常!")
    except:
    print("捕获所有异常!")

    try:
    print(ex)
    except (NameError, IOError) as e: # 同时捕获多个异常, 并告知异常原因
    print("捕获多个异常!")
    print(e)
    except:
    print("捕获所有异常!")


    # - try except else -
    try:
    pass
    except:
    print("捕获异常!")
    else: # 没有发生异常是执行该代码块
    print("运行正常.")

    # - try except else finally -
    try:
    pass
    except:
    print("捕获异常!")
    else:
    print("运行正常.")
    finally: # 不管是否发生异常都执行
    print("不管是否发生异常都执行")




    # === 自定义异常 ===
    # - 编写自定义异常 -
    class MyError(Exception): # 继承 Exception

    # 重写并super构造
    def __init__(self, mes = "抛出一个异常."):
    Exception.__init__(self)
    self.message = mes

    def __str__(self):
    return self.message


    # - 使用自定义异常 -
    def myerr():
    try:
    raise MyError("抛异常!") # 抛出异常
    except MyError as e: # 接住异常
    print(e)


    # === 断言语句 ===
    def assertdemo():
    # 断言一般用于测试, 如果测试结果为Flase,将抛出AssertionError异常
    assert type("string") is str
    assert 3 < 4
    assert 3 > 4, "AssertionError异常"

    # ======= 函数调用 ======
    if __name__ == "__main__":
    excep()
    myerr()
    assertdemo()
  • 相关阅读:
    HDU 1069 Monkey and Banana
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    Gym100923H Por Costel and the Match
    Codeforces 682C Alyona and the Tree
    Codeforces 449B Jzzhu and Cities
    Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes
    Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum
    Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板
    快乐数问题
  • 原文地址:https://www.cnblogs.com/vito13/p/7730058.html
Copyright © 2011-2022 走看看