簡體   English   中英

如果到達代碼中的非法位置,將引發哪個異常?

[英]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'

或類似的東西。

我建議使用SystemErrorEnvironmentError但也將方便使用IOErrorLookupError

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM