![](/img/trans.png)
[英]Which exception should I raise on bad/illegal argument combinations in Python?
[英]Which exception to raise if arrived at an illegal location in code?
我的代码需要处理文件或目录(从shell接受它们中的任何一个作为argparse
参数)。
相关代码段是:
if in_dir:
in_glob = os.path.join(in_dir, "*." + ext)
elif in_file:
in_glob = in_file
else:
# Should never arrive here!
print("error: neither input file, nor input directory are set.")
raise SystemExit
在else
节(永远不应输入)中,我想提出比SystemExit
更具信息性的异常-表示控制流到达了非法位置。
您会建议我提出哪个例外?
许多其他语言为此目的提供了assert_not_reached
函数,而python没有。 通常用
assert False, 'this should never be reached'
或类似的东西。
我建议使用SystemError
或EnvironmentError
但也将方便使用IOError
或LookupError
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.