Is exception handling always expensive?

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.

+4
source share
3 answers

, " " .

, 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, . , , .

+6

, .

Python "". , , , ( Python , "split" __getattr__, if, split, , , B).

Python , , "".

+2

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.

0
source

Source: https://habr.com/ru/post/1525910/


All Articles