[Python-ideas] Exception for developer errors?

Chris Angelico rosuav at gmail.com
Wed Apr 10 18:51:06 EDT 2019


On Thu, Apr 11, 2019 at 8:15 AM Jeroen Demeyer <J.Demeyer at ugent.be> wrote:
>
> On 2019-04-11 00:09, Stefano Borini wrote:
> > I occasionally found situations where I want to raise an exception for
> > errors that can only arise because the developer made a mistake, for
> > example:
>
> I use AssertionError for this. An assertion failure means "this is a
> bug", so that seems the right choice to me. You don't need to use an
> actual assert statement, you can manually raise AssertionError too.
>

Agreed. It's worth noting that AssertionError isn't affected by the -O
flag - only the assert *statement*. Also, anything that says "except
AssertionError:" (outside of unit testing) should be considered a
major bug, which in turn means that this should *only* be raised when
you truly expect that normal usage cannot ever hit this. Which is
perfect for the use-case you describe.

ChrisA


More information about the Python-ideas mailing list