Do you think it is better to use conditions in the code or handle exceptions?

Example of using the condition:

import os if os.path.exists("путь"): pass 

Exception handling example:

 try: pass except FileNotFoundError: pass 

5 answers 5

The answer @Oma is not quite correct, if only because going beyond the bounds of the list (in pure Python there are no arrays in their usual sense), an IndexError exception will be raised. If we talk about pure Python, and not about modules, then, in my opinion, there are always exceptions thrown when you or your program do something wrong.

The official Python documentation has the phrase 'Easier to ask for forgiveness than permission' - often it’s really easier and more readable. Exceptions will be useful if you need to insure yourself from something truly unforeseen; if you need to flip the error up to several levels of logic; if checking everyone before you do something is too expensive.

For example, if you open a file once in your program, it will be much easier to wrap the opening in try/except than to check if it exists, whether it has read / write permissions, etc. If a large amount of data arrives at your input, and you need to validate it before processing, and you expect a small percentage of invalid data, it is very likely that it will be too expensive to check it every time. Catch the exception fit better.

On the other hand, if in the last example of invalid data 50% is expected, it is likely that validation will be cheaper than catching exceptions (this is quite an expensive procedure).

    Using try/except besides the advantages listed by @PavelGurkov also excludes the possibility of racing between processes, because when checking for existence and before opening / deleting / any other operation, another stream or program can open / delete / move a file. Therefore, definitely try..except

      Your question has already been.

      You should prefer try/except over if/else if that results in

      speed-ups (for example by look extras)

      Often, these go hand-in-hand.

      https://stackoverflow.com/questions/7604636/better-to-try-something-and-catch-the-exception-or-test-if-its-possible-first

        A rather detailed answer as to whether it is better to choose: try..except or try..except and whether this affects speed .

        In short: try except in "python" does not work like in c++ or java - it is quite fast, however, catching exceptions is still a rather expensive operation and if the logic in this place is built so that the excitation of exceptions is possible often and there is a desire to take care of a little about speed, it probably makes sense to use some clarity if..else . On the other hand, as the author rightly notes:

        But mostly, don't worry about speed. If you are worried about, you shouldn't be using Python in the first place. Python is optimized for developer productivity, not execution speed. Write your code, if it is too slow, then worry about finding them.

        • Where does this stereotype come from, that in c ++ exceptions are somehow slow? - Vladimir Gamalyan
        • No stereotypes, meaning that in c ++ if..else is faster than try..catch. - Karimov Dmitriy
        • How did you come to this conclusion? - Vladimir Gamalyan
        • one
          I had heard that c ++ uses tricky mechanisms at the compilation stage, with special transition tables crammed into a binary file and God knows what for later processing in case of an exception. It is completely not the fact that they are slow even in comparison with if/else . - Pavel Gurkov

        Not all your possible errors in the code go into exceptions, for example, such as going beyond the array, etc. Rather, it must be combined for greater safety. PS A good code is the code that takes into account all sorts of possible developments.

        • 7
          Going beyond the bounds of the array still throws an exception - andreymal