TransWikia.com

Python вывод ошибки в except

Stack Overflow на русском Asked by Zamur on December 26, 2021

Использую Python 3.6.8

Интересует как вывести внутри except причину ошибки.

Надеюсь объяснил понятно

2 Answers

In [7]: import traceback

In [8]: try:
   ...:     5 / 0
   ...: except ZeroDivisionError as e:
   ...:     traceback.print_exc()
   ...:
Traceback (most recent call last):
  File "<ipython-input-8-7e0b4de302f2>", line 2, in <module>
    5 / 0
ZeroDivisionError: division by zero

В отличии от print(e), print_exc выводит немного больше информации о том что случилось.

Answered by Pavel Durmanov on December 26, 2021

Вот так вот:

try:
  0 / 0
except Exception as e: 
  print(e)

Answered by dIm0n on December 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP