473,386 Members | 1,766 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,386 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 1866
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.