Python: is RuntimeError acceptable for general use?

Question:

Is it acceptable to use the RuntimeError exception for general application use?

raise RuntimeError('config file is missing host address')

I’ve got some code with a couple of one-off situations like this and would prefer to avoid creating one-off exception classes for each of them. All the situations are fatal, and my goal is to get a clear message to the console. Basically I’m looking for something similar to the deprecated

raise 'config file is missing host address'
Asked By: Mark Harrison

||

Answers:

This is… OK. Ideally, you would have separate exceptions for each reasonably distinct situation (e.g. one exception for all “the config file is malformed” errors, reuse FileNotFoundError in 3.x for “the config file doesn’t exist”, etc.). But this is one of the more innocuous forms of technical debt.

The downside is that if you ever do introduce those separate exceptions, they may need to subclass from RuntimeError for reasons of backwards compatibility. That’s kind of ugly, but mostly harmless.

Answered By: Kevin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.