![](/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.