Python: Is it bad form to raise exceptions within __init__?

Raising exceptions within __init__() is absolutely fine. There’s no other good way to indicate an error condition within an initializer, and there are many hundreds of examples in the standard library where initializing an object can raise an exception.

The error class to raise, of course, is up to you. ValueError is best if the initializer was passed an invalid parameter.

Leave a Comment