PyQt 文件错误处理的各种方法

PyQt 文件错误处理的各种方法

记录博文

第一种

错误处理

error = None
fh = None
try:
	# open file and read data
except (IOError, OSError) as e:
	error = e
finally:
	if fh is not None:
		fh.close()
	if error is not None:
		return False, error
	return True, "Success"

调用方式

ok, msg = load(args)
if not ok:
	QMessageBox.warning(self, "File Error", msg)

第二种

错误处理

exception = None
fh = None
try:
	# open file and read data
except (IOError, OSError) as e:
	exception = e
finally:
	if fh is not None:
		fh.close()
	if error is not None:
		raise exception

调用方式

try:
	load(args)
except (IOError, OSError) as e:
	QMessageBox.warning(self, "File Error", e)

第三种

错误处理

fh = None
try:
	# open file and read data
except (IOError, OSError) as e:
	QMessageBox.warning(self, "File Error", e)
finally:
	if fh is not None:
		fh.close()

调用方式

load(args)

点我回顶部

 
 
 
 
 
 
 
Fin.

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/109399689
今日推荐