<div dir="auto">You have AssertionError for that.<div dir="auto"><div dir="auto"><br></div><div dir="auto">Elazar</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">בתאריך יום ה׳, 11 באפר׳ 2019, 1:10, מאת Stefano Borini ‏<<a href="mailto:stefano.borini@gmail.com">stefano.borini@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I occasionally found situations where I want to raise an exception for<br>
errors that can only arise because the developer made a mistake, for<br>
example:<br>
<br>
- an abstract method is supposed to be reimplemented and its execution<br>
is supposed to leave some internal constraints of an object unchanged,<br>
but these are instead violated.<br>
- an impossible "else" condition after an if/elif, where the else<br>
cannot simply happen unless someone really screwed up the internal<br>
state of the object.<br>
<br>
In general, when these cases happen, I use RuntimeError, but other<br>
people may choose otherwise. I've also seen ValueError, plain<br>
Exception or a specifically made subclass used in these cases.<br>
Whatever the choice is, it generally lacks clarity of communication to<br>
whoever receives it.<br>
RuntimeError is a rather generic exception according to the<br>
documentation, and you can only rely on the message, which relies on<br>
writing something appropriate for the situation:<br>
<br>
```<br>
exception RuntimeError<br>
Raised when an error is detected that doesn’t fall in any of the other<br>
categories. The associated value is a string indicating what precisely<br>
went wrong.<br>
```<br>
<br>
while it would be useful to communicate clearly to whoever is using or<br>
modifying the code "listen, it's your mistake, you misunderstood how I<br>
work or you ruined my internals, leading me to an impossible state".<br>
Also, customers seeing this kind of exception would understand without<br>
a doubt that the application is broken because of an internal error.<br>
<br>
I tried some search on the mailing list but could not find anything at<br>
a glance about this topic. Was this already discussed in the past?<br>
<br>
Thanks<br>
<br>
-- <br>
Kind regards,<br>
<br>
Stefano Borini<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank" rel="noreferrer">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>