标签:错误 ISE 数据 行数据 color none ini cep 文件中
import os class ExistsError(Exception): # 自定义异常,将要定义的异常继承Exception,就可以在捕捉异常那里捕捉到 pass class KeyInvalidError(Exception): pass def new_func(path,prev): """ 去path路径的文件中,找到前缀为prev的一行数据,获取数据并返回给调用者 1000,成功 1001,文件不存在 1002,关键字为空 1003,位置错误 :param path: :param prev: :return: """ response = {‘code‘:1000,‘date‘:None} try: if not os.path.exists(path): raise ExistsError() if not prev: raise KeyInvalidError() except ExistsError as e: response[‘code‘]=1001 response[‘date‘]= "文件不存在" except KeyInvalidError as e: response[‘code‘] = 1002 response[‘date‘] = "关键字为空" except Exception as e: response[‘code‘] = 1003 response[‘date‘] = "未知错误" return response
class MyExceptError(Exception): def __init__(self,key,value): self.key = key self.value = value try: raise MyExceptError(‘1001‘,"关键字为空") except MyExceptError as e: print(e)
标签:错误 ISE 数据 行数据 color none ini cep 文件中
原文地址:https://www.cnblogs.com/whileke/p/11402677.html