473,224 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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

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?

tks in advance.

daniel

Sep 30 '06 #1
6 1858
"daniel" <da*************@gmail.comwrites:
I'm expecting the exception to be caught silently, and print the msg to
some place
Then why not use logging.error() ?

<URL:http://docs.python.org/lib/module-logging>
=====
error(msg[, *args[, **kwargs]])
Logs a message with level ERROR on the root logger. The arguments
are interpreted as for debug().

exception(msg[, *args])
Logs a message with level ERROR on the root logger. The arguments
are interpreted as for debug(). Exception info is added to the
logging message. This function should only be called from an
exception handler.
=====

A 'try ... except' statement is not an exception handler. So, if you
want to log the fact that an error occurred, it seems logging.error()
is the correct choice.

--
\ "I have one rule to live by: Don't make it worse." -- Hazel |
`\ Woodcock |
_o__) |
Ben Finney

Sep 30 '06 #2
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
Sep 30 '06 #3
thank your so much firstly.

I feel really ashamed...

I think I did read through all examples and docs, but I just took for
granted that all the info(), warning(), debug() like functions behaves
much alike except the level name.. so the description you listed really
was ignored,,

tks again for your help.. I do appreciate it.

daniel

Sep 30 '06 #4
Ben Finney wrote:
[...]
A 'try ... except' statement is not an exception handler. [...]
Just as a matter of interest, what would your definition of an exception
handler be, then? Specifically, what's the "except" clause for?

The docs for looging.except should make it explicit that the exception
will be re-raised.

Of course it might be possible to do something hackish like

try:
...
except:
try:
logging.exception(...)
except:
pass

which (although untested) should theoretically allow the catching (for a
second time) of teh exception reraised by logging.exception().

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 30 '06 #5
Steve Holden wrote:
Ben Finney wrote:
[...]
>A 'try ... except' statement is not an exception handler. [...]

Just as a matter of interest, what would your definition of an exception
handler be, then? Specifically, what's the "except" clause for?

The docs for looging.except should make it explicit that the exception
will be re-raised.

Of course it might be possible to do something hackish like

try:
...
except:
try:
logging.exception(...)
except:
pass

which (although untested) should theoretically allow the catching (for a
second time) of teh exception reraised by logging.exception().
Use the source, people. The exception is not reraised. The only effect of
exception() versus error() is that exception() passes the "exc_info=1" keyword
argument to error() which means that the stack trace is added to the
logging message.

The default logger prints to stdout, which is why the stack trace is printed
there too.

(In the sense of the logging docs, an except:-clause *IS* an error handler).

Georg
Sep 30 '06 #6
Steve Holden <st***@holdenweb.comwrites:
Ben Finney wrote:
[...]
A 'try ... except' statement is not an exception handler. [...]

Just as a matter of interest, what would your definition of an
exception handler be, then? Specifically, what's the "except" clause
for?
It seems my understanding was wrong. The 'except' clause *is* an
exception handler. (I was thinking that the "default exception
handler" was the only thing that could be called an "exception
handler".) It's explained better here:

<URL:http://docs.python.org/ref/try.html>
The docs for looging.except should make it explicit that the
exception will be re-raised.
Yes, I agree; it wasn't very clear on reading the description of
'logging.exception()' what would actually happen.
Of course it might be possible to do something hackish like

try:
...
except:
try:
logging.exception(...)
except:
pass

which (although untested) should theoretically allow the catching
(for a second time) of teh exception reraised by
logging.exception().
It does seem rather a side effect, and an unexpected (and
undocumented) one at that.

--
\ "If it ain't bust don't fix it is a very sound principle and |
`\ remains so despite the fact that I have slavishly ignored it |
_o__) all my life." -- Douglas Adams |
Ben Finney

Oct 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave | last post by:
Hello all, The code below is not legal (problem with the foo_t initializer list) because: "A reference that is not to 'const' cannot be bound to a non-lvalue" How can I best achieve an...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
26
by: Dragon | last post by:
I have an Access 2003 .mde sitting on an SQL Server. The tables for the application also sit on the Server. I'm having a problem with ODBC on only one of about 10 machines. All the other machines...
2
by: mahesh.mohan1 | last post by:
Hi All, I am trying to deploy one of the portal applications on websphere 6.1. During the course of deployment, I get the following error: EJPPC0005E: portlet.xml validation caught a...
17
by: > Adrian | last post by:
I have converted a number of applications to enable them to work together on a network. I have been led to believe that I can do this as follows: FileStream fs = new FileStream(some code); while...
18
by: Franz Steinhaeusler | last post by:
Hello, I did not find any reasonable pyhton source code beautifier program (preferable gui). Some would ask why? Program it immediatly good. (BTW: Would be a nice project, if I would have more...
3
by: =?Utf-8?B?RHVrZSAoQU4yNDcp?= | last post by:
The majority of pages on our site need authentication (forms auth against the aspnetdb database). I created an '~/auth' folder with its own config file forcing authentication for any pages in the...
7
by: Brent | last post by:
Page1.aspx calls Page2.aspx like this: ---------------- Server.Execute("Page2.aspx"); --------------- Page2.aspx's Page_Load event calls a function, getMsgs().This function runs normally...
176
by: . | last post by:
9/11 Mysteries http://video.google.com/videoplay?docid=-8172271955308136871 http://www.911weknow.com Ignore those who would go to great effort and expend much of heir time in poo-pooing this...
2
by: John [H2O] | last post by:
Just a quick question.. what do I need to do so that my print statements are caught by nohup?? Yes, I should probably be 'logging'... but hey.. Thanks! -- View this message in context:...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.