In Python, is it possible to print exceptions even when they are being ignored? -
i'm aware it's possible ignore exceptions in python using try...except statements. possible ignore exceptions in python when occur, still print them?
i tried ignoring exception here, , therefore, exception not printed when encountered:
try: num = 0 if num == 0: raise exception("num must not 0!") except exception: pass '''the exception ignored, , not printed.'''
i've written simple source-to-source compiler has lot of exceptions these, , i'm not sure how can ignore exceptions while still printing them. how can ensure exceptions printed console when being ignored?
you can print exception this.
try: x = 1 / 0 except exception e: print e
edit:
as user1354557, gcbirzan, , jonathan vanasco pointed out, can use traceback
, logging
modules more precise error messages. error messages printed out these ways more verbose, (usually) thing.
import traceback try: x = 1 / 0 except exception e: print traceback.format_exc() # prefer traceback.print_exc() import logging try: x = 1 / 0 except exception e: logging.exception(e)
Comments
Post a Comment