缺少设置文件的适当Python异常类

jdg*_*jdg 8 python error-handling

什么是适当的Python异常提高,如果有一个缺少设置文件?

例如,在Django项目中,允许用户定义本地设置的轻量级方法是将以下代码段添加到settings.py文件中

try:
    from local_settings import *
except ImportError:
    # want to add informative Exception here
    pass
Run Code Online (Sandbox Code Playgroud)

因此,任何本地设置都会覆盖settings.py中的默认值.

Ray*_*ger 5

The usual exception for missing files is IOError.

You can customize the wording by creating a subclass:

class MissingSettingsFile(IOError):
    'Missing local settings file'
    pass
Run Code Online (Sandbox Code Playgroud)

Then, use that custom exception in your code snippet:

try:
    from local_settings import *
except ImportError:
    raise MissingSettingsFile
Run Code Online (Sandbox Code Playgroud)