I was told that exception handling for operations such as type determination is a bad form, since exceptions are always expensive. However, I saw posts (especially those related to Python, such as an answer to this , that advise using exception handling specifically for this purpose.
Then I was wondering if you should avoid throwing and catching exceptions because it is always expensive to compute, or some languages like Python handle exceptions better, and it is allowed to use exception handling more liberally.
, " " .
, Python , , ++. Python . " , ", : , , , .
:
try: do_something_with(dict["key"]) except (KeyError, TypeError): # Oh well, there is no "key" in dict, or it has the wrong type
if hasattr(dict, "__getitem__") and "key" in dict: do_something_with(dict["key"]) else: # Oh well
, Python for : StopIteration, . , , .
for
StopIteration
, .
Python "". , , , ( Python , "split" __getattr__, if, split, , , B).
__getattr__
Python , , "".
tco . 1.0.1alpha . ; , , .
, , , , . , , , . catching!
For this reason, I decided to remove the first system when releasing the version of my module 1.1. Although a little slower, I found that the system relying on exceptions was more reliable, and I focused on it.
Source: https://habr.com/ru/post/1525910/More articles:Implementing linear regression is always worse than sklearn - pythonCould not find IdentityUser namespace name - c #JavaFx Scene Designer Choose chart axis type of chart - javafxDimiss popup when touching outside - androidНайти максимальный элемент в двоичном дереве - javaEdge snap effect - javaКак сделать полный html5 экран в Libgdx? - browserd3.js alternative to .tickSubdivide axis? - d3.jsPip not working on Cygwin - pythonDetailed message: The error was caused by libgit2. Category = index (error) - gitAll Articles