添加异常处理

This commit is contained in:
SiYuan 2024-02-08 21:18:27 +08:00
parent 610cd4673b
commit e817b868ba
2 changed files with 51 additions and 1 deletions

View File

@ -0,0 +1,49 @@
import sqlite3
import sys
import traceback
class ExceptionHanding:
def __init__(self, exc_type, exc_value, traceback_):
self.exc_type = exc_type
self.exc_value = exc_value
self.traceback = traceback_
self.error_message = ''.join(traceback.format_exception(exc_type, exc_value, traceback_))
def parser_exc(self):
if isinstance(self.exc_value, PermissionError):
return f'权限错误,请使用管理员身份运行'
elif isinstance(self.exc_value, sqlite3.DatabaseError):
return '数据库错误请删除app文件夹后重启电脑再运行软件'
elif isinstance(self.exc_value, OSError) and self.exc_value.errno == 28:
return '空间磁盘不足,请预留足够多的磁盘空间以供软件正常运行'
elif isinstance(self.exc_value, TypeError) and 'NoneType' in str(self.exc_value) and 'not iterable' in str(
self.exc_value):
return '数据库错误请删除app文件夹后重启电脑再运行软件'
else:
return '未知错误类型,可参考 https://blog.lc044.love/post/7 解决该问题'
def __str__(self):
errmsg = f'{self.error_message}\n{self.parser_exc()}'
return errmsg
def excepthook(exc_type, exc_value, traceback_):
# 将异常信息转为字符串
# 在这里处理全局异常
error_message = ExceptionHanding(exc_type, exc_value, traceback_)
txt = '您可添加QQ群发送log文件以便解决该问题'
msg = f"Exception Type: {exc_type.__name__}\nException Value: {exc_value}\ndetails: {error_message}\n\n{txt}"
print(msg)
# 调用原始的 excepthook以便程序正常退出
sys.__excepthook__(exc_type, exc_value, traceback_)
# 设置 excepthook
sys.excepthook = excepthook
result = None # 假设这里返回了一个 None
for item in result: # 尝试迭代一个 None
print(item)

View File

@ -3,6 +3,7 @@ import sys
import time import time
import traceback import traceback
from app.log.exception_handling import ExceptionHanding
from app.ui.Icon import Icon from app.ui.Icon import Icon
widget = None widget = None
@ -13,7 +14,7 @@ def excepthook(exc_type, exc_value, traceback_):
# 在这里处理全局异常 # 在这里处理全局异常
error_message = ''.join(traceback.format_exception(exc_type, exc_value, traceback_)) error_message = ExceptionHanding(exc_type, exc_value, traceback_)
txt = '您可添加QQ群发送log文件以便解决该问题' txt = '您可添加QQ群发送log文件以便解决该问题'
msg = f"Exception Type: {exc_type.__name__}\nException Value: {exc_value}\ndetails: {error_message}\n\n{txt}" msg = f"Exception Type: {exc_type.__name__}\nException Value: {exc_value}\ndetails: {error_message}\n\n{txt}"
logger.error(f'程序发生了错误:\n\n{msg}') logger.error(f'程序发生了错误:\n\n{msg}')