The second example is bad. You take a lot of overhead to catch the exception when, as you demonstrate, it is just as easy to prevent the exception in the first place. In addition, you also assume that you know why this exception was thrown - if there were some other exception, for example, from memory or something else, you report it as a “field not found”, even if it is not So.
Keep in mind that try / catch in languages like C ++ and Java are very expensive because of the state that they need to save and restore. Python, on the other hand, has very cheap exceptions, and they positively recommend that you use try / except for a simple check. But even in this case, catching everything and pretending that one kind of exception is still bad.
Paul tomblin
source share