Z
Z
zaychonok_kisa2021-11-01 12:04:22
Python
zaychonok_kisa, 2021-11-01 12:04:22

How to make an exception (try except) thrown every time an error occurs?

Hello. need help)
I need to handle divide-by-zero error. And then in another task, you need to display a variable that refers to this error .. In short, here:
-------------------------------- ------------------

try:
  a = 5/0
  print(a)
except:
    print("Деление на 0")
    print("Исключение было обработано")

#если я встречаю в коде где то эту ошибку и вывожу ее
#как ее обработать
print(a)

--------------------------------------------
A outputs:
Деление на 0
Исключение было обработано
Traceback (most recent call last):
  File "try_except.py", line 15, in <module>
    print(a)
NameError: name 'a' is not defined

-------------------------------------------------- -
In short, he processes the first time and not the second time. Is there a way to make it work all the time?
I can't find anything on the internet. Can you post a link? Please
Or is it necessary to write like this every time? (try except)

------------------------------
Well, give me a hint, at least for God's sake, who doesn't feel sorry for

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jan, 2021-11-01
@zaychonok_kisa

zaychonok_kisa , you have nothing to display in "a". 5 divided by 0, what should be output? Zero? If so then:

try:
  a = 5/0
except ZeroDivisionError:
    a = 0
    print("Деление на 0")
    print("Исключение было обработано")

#если я встречаю в коде где то эту ошибку и вывожу ее
#как ее обработать
print(a)

Conclusion:
Divide by 0
Exception handled
0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question