Какие исключения Python обычно следует перехватывать, поскольку следует избегать широкого перехвата исключений ⇐ Python
Какие исключения Python обычно следует перехватывать, поскольку следует избегать широкого перехвата исключений
This question is about the W0718:broad-exception-caught https://pylint.readthedocs.io/en/latest ... aught.html We should NOT do this
except Exception: except: Because exceptions like SyntaxError and MemoryError or SyntaxError, KeyboardInterrupt should not be caught, as discussed here Should I always specify an exception type in `except` statements?.
However, there are many exceptions, e.g. ArithmeticError, BufferError, LookupError, GeneratorExit, AssertionError.... maybe 30+
If I should not catch broad-exception, what exactly should I do if I do not want the program to crush?
Assume I was doing a general computation/crawling, what errors should I normally catch? Is the best practice to cluster a few errors and define them as a constant?
PEP 3151 – Reworking the OS and IO exception hierarchy https://peps.python.org/pep-3151/
e.g. for internet connection: INTERNET_ERRORS = (ConnectionError, TimeoutError, BrokenPipeError, HTTPError)
or more commonly catch
GRAMMA_ERRORS = (IndexError, AssertionError, AttributeError, KeyError, NameError, TypeError, ZeroDivisionError, RuntimeError, NotImplementedError) What do you usually do?
Thanks!
Источник: https://stackoverflow.com/questions/780 ... caught-sho
This question is about the W0718:broad-exception-caught https://pylint.readthedocs.io/en/latest ... aught.html We should NOT do this
except Exception: except: Because exceptions like SyntaxError and MemoryError or SyntaxError, KeyboardInterrupt should not be caught, as discussed here Should I always specify an exception type in `except` statements?.
However, there are many exceptions, e.g. ArithmeticError, BufferError, LookupError, GeneratorExit, AssertionError.... maybe 30+
If I should not catch broad-exception, what exactly should I do if I do not want the program to crush?
Assume I was doing a general computation/crawling, what errors should I normally catch? Is the best practice to cluster a few errors and define them as a constant?
PEP 3151 – Reworking the OS and IO exception hierarchy https://peps.python.org/pep-3151/
e.g. for internet connection: INTERNET_ERRORS = (ConnectionError, TimeoutError, BrokenPipeError, HTTPError)
or more commonly catch
GRAMMA_ERRORS = (IndexError, AssertionError, AttributeError, KeyError, NameError, TypeError, ZeroDivisionError, RuntimeError, NotImplementedError) What do you usually do?
Thanks!
Источник: https://stackoverflow.com/questions/780 ... caught-sho
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Зачем перехватывать исключения в Java, если можно перехватывать Throwables?
Anonymous » » в форуме JAVA - 0 Ответы
- 24 Просмотры
-
Последнее сообщение Anonymous
-