1. 3
  1.  

  2. 3

    This is all “you should not do this” without giving too much reason. I think it’s perfectly fine to throw an exception when you pass a negative value to a sqrt as that enables you to catch the exception.

    1. 3

      The articles advice may apply to C++ but doesn’t apply to Python, which is intentionally designed to use exceptions as control flow. For example, it detects when to leave a for loop by catching a StopIteration exception.

      1. 2

        I think this advice could be more language-specific. For instance, exceptions can be pretty dangerous in C++, but less-so in Python.

        [1] https://google.github.io/styleguide/cppguide.html#Exceptions

        1. 2

          This is a “certain specific languages’ implementations of exceptions” approach. And even with the explicit bits about Python now removed (after being called out in other comments), the idea that exceptions are only for “situations when a system cannot accomodate a valid request because of a dynamic external fault condition” is very much not a cross-language truth.

          It’s true that in some languages exceptions are looked on as so awful and painful and expensive that you should do almost anything to avoid throwing one. But in other languages… they’re not. For example, the advice given here about when to use or not use exceptions in C++ not only is wrong for Python, it’s wrong for Java and C#. The existence of Java’s IllegalArgumentException, for example, contradicts this article’s advice that exceptions are not to be used for signaling an invalid input.

          So generalizing from a single “exceptions bad, never throw if you can avoid it” language to all languages is a bad idea. Better to learn how the particular language you’re working with treats exceptions, and code to that standard, than attempt to give language-independent advice (which will always fail because there is no generalizable cross-language advice).