#异常机制
# try:
# a=int(input("请输入:"))
# # print(b/a)#触发未知错误
# print(1/a)
# except ZeroDivisionError as e1:#取别名,方便打印,分母不能为0 division by zero
# print("分母不能为0",e1)
# except ValueError:#值异常
# print("你输入的不是数字")
# except:
# print("未知错误")#除了上面的其他错误
# else:
# print("程序没有错误")#程序正确运行会执行一次,如果程序错误,不会执行
# finally:
# print("程序运行完毕")#程序执行完毕会执行一次,不管程序错误还是正确
#日志模块
import logging
# logging.debug("---------- debug")
# logging.info("===============info")
# logging.warning("=============warning")#默认打印warning及以上的级别
# logging.error("============error")
# logging.critical("============critical")
# logging.basicConfig(level="DEBUG")#打印debug及以上的级别
# logging.debug("---------- debug")
# logging.info("===============info")
# logging.warning("=============warning")#默认打印warning及以上的级别
# logging.error("============error")
# logging.critical("============critical")
import time
# print(time.strftime("%y-%m-%d %H:%M:%S"))
# logging.basicConfig(level="DEBUG",filename="./log.log")#把debug及以上的级别写入到log文件中
# logging.debug(time.strftime("%y-%m-%d %H:%M:%S")+"---------- debug")
# logging.info("===============info")
# logging.warning("=============warning")#默认打印warning及以上的级别
# logging.error("============error")
# logging.critical("============critical")
logging.basicConfig(level="DEBUG",filename="./log.log",filemode="a")
import traceback
# try:
# a=int(input("请输入:"))
# # print(b/a)#触发未知错误
# print(1/a)
# except ZeroDivisionError as e1:#取别名,方便打印,分母不能为0 division by zero
# print("分母不能为0",e1)
# logging.debug(time.strftime("%y-%m-%d %H:%M:%S")+" "+"分母不能为0"+traceback.format_exc()+str(e1))
# except ValueError:#值异常
# print("你输入的不是数字")
# logging.debug(time.strftime("%y-%m-%d %H:%M:%S")+" "+"你输入的不是数字"+traceback.format_exc())
# except:
# print("未知错误")#除了上面的其他错误
# else:
# print("程序没有错误")#程序正确运行会执行一次,如果程序错误,不会执行
# finally:
# print("程序运行完毕")#程序执行完毕会执行一次,不管程序错误还是正确
#主动抛出异常,使用raise关键字抛出需要的异常
# try:
# raise ZeroDivisionError
# except:
# print("fen分母不能为0")
#所有异常的父类或者祖类都是exception
# (<class 'ArithmeticError'>,)
# (<class 'Exception'>,)
# (<class 'Exception'>,)
# (<class 'BaseException'>,)
# (<class 'object'>,)
# print(ZeroDivisionError.__bases__)
# print(ArithmeticError.__bases__)
# print(ValueError.__bases__)
# print(Exception.__bases__)
# print(BaseException.__bases__)
#异常的类型
ZeroDivisionError,ValueError,IOError,IndexError,FileNotFoundError,NameError
# list1=[1,3,5]
# print(list1[5])#IndexError: list index out of range
# with open("d:/abcde.txt") as f:#FileNotFoundError: [Errno 2] No such file or directory: 'd:/abcde.txt'
# f.read()
# print(g)#NameError: name 'g' is not defined
#断言
assert 1==1#AssertionError,只对断言错误进行报错
assert isinstance(1,float)