why logging re-raise my exception and can't be caught?

Diez B. Roggisch deets at nospam.web.de
Sat Sep 30 04:05:37 EDT 2006


daniel schrieb:
> I use a simple program to illustrate the problem:
> 
> import logging
> 
> def foo() :
>     raise ValueError("foo")
> 
> if __name__ == "__main__" :
>     try :
>         foo()
>     except ValueError :
>         logging.exception("caught here")  -- seems re-raise the
> exception and can't be caught
>         print "caught here" --- just works.
> 
> I'm expecting the exception to be caught silently, and print the msg to
> some place, if I comment out the logging statement, it just works, but
> now, the stack trace is printed as if the except statement was not
> there!!
> 
> how could this happen, just weird.. can I make the logging module
> behave as I expected?

For starters - reading the documentation. The logging.exception-call 
precisely is for printing the exception as if it was not caught at all.

If you don't want that, don't use it - use one of the error, warn, info 
or debug calls.

Diez



More information about the Python-list mailing list