Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TypeError not caught by except statement

9 views
Skip to first unread message

siddu

unread,
Jan 25, 2010, 3:35:48 AM1/25/10
to

Hi,

except not able to caught the TypeError exception occured in the below
code

log.info("refer",ret) in the try block

throws a TypeError which is not caught .
Also sometimes process is getting hanged.

------------------------------------------------------------------------------------------------------------------------------------------------
import logging
log = logging.getLogger()
fileName = strftime("%d-%b-%Y-", gmtime()) + str(int(time.time())) + "-
Log.log"
log = logging.getLogger()
log.setLevel(logging.NOTSET)
fh = logging.FileHandler(logFile)
logFileLevel = logging.DEBUG
fh.setLevel(logFileLevel)
format_string = '%(process)d %(thread)d %(asctime)-15s %(levelname)-5s
at %(filename)-15s in %(funcName)-10s at line %(lineno)-3d "%(message)
s"'
fh.setFormatter(logging.Formatter(format_string))
log.addHandler(fh)

try:
log.info("start")
log.info("refer",ret)
log.info("end")
except TypeError:
log.exception("Exception raised")

----------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT message:

Traceback (most recent call last):
File "C:\Python26\lib\logging\__init__.py", line 768, in emit
msg = self.format(record)
File "C:\Python26\lib\logging\__init__.py", line 648, in format
return fmt.format(record)
File "C:\Python26\lib\logging\__init__.py", line 436, in format
record.message = record.getMessage()
File "C:\Python26\lib\logging\__init__.py", line 306, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting

Alf P. Steinbach

unread,
Jan 25, 2010, 3:55:45 AM1/25/10
to
* siddu:

> Hi,
>
> except not able to caught the TypeError exception occured in the below
> code
>
> log.info("refer",ret) in the try block
>
> throws a TypeError which is not caught .
> Also sometimes process is getting hanged.
>
> ------------------------------------------------------------------------------------------------------------------------------------------------
> import logging
> log = logging.getLogger()
> fileName = strftime("%d-%b-%Y-", gmtime()) + str(int(time.time())) + "-
> Log.log"
> log = logging.getLogger()
> log.setLevel(logging.NOTSET)
> fh = logging.FileHandler(logFile)

Where does 'logFile' come from.


> logFileLevel = logging.DEBUG
> fh.setLevel(logFileLevel)
> format_string = '%(process)d %(thread)d %(asctime)-15s %(levelname)-5s
> at %(filename)-15s in %(funcName)-10s at line %(lineno)-3d "%(message)
> s"'
> fh.setFormatter(logging.Formatter(format_string))
> log.addHandler(fh)
>
> try:
> log.info("start")
> log.info("refer",ret)
> log.info("end")
> except TypeError:
> log.exception("Exception raised")

Try to reduce the example to a small, complete program that exhibits the problem.

>
> ----------------------------------------------------------------------------------------------------------------------------------------------
> OUTPUT message:
>
> Traceback (most recent call last):
> File "C:\Python26\lib\logging\__init__.py", line 768, in emit
> msg = self.format(record)
> File "C:\Python26\lib\logging\__init__.py", line 648, in format
> return fmt.format(record)
> File "C:\Python26\lib\logging\__init__.py", line 436, in format
> record.message = record.getMessage()
> File "C:\Python26\lib\logging\__init__.py", line 306, in getMessage
> msg = msg % self.args
> TypeError: not all arguments converted during string formatting

Is this a complete listing?


CHheers & hth.,

- Alf

tec

unread,
Jan 25, 2010, 4:56:56 AM1/25/10
to
~~~~~~~~~~~~~~~~~~~~~~~
Seems this line causes the exception, and it is handled inside
log.info(), which prints those traceback info.
Usually log.info(msg, args) raises the same exception as print(msg%args).

Carl Banks

unread,
Jan 25, 2010, 5:03:49 AM1/25/10
to

The logging module is swallowing the exception (and printing a
traceback), I guess because logging is considered something that
shouldn't bring your program down on error. You can apparently define
a logging handler that overrides "handleError" to propogate the
exception if you want.

Can't tell you why it's hanging, but the logging error you're getting
is probably because your string formatter is trying to perform the
following operation:

"refer" % (ret,)


Carl Banks

Steve Holden

unread,
Jan 26, 2010, 11:54:23 AM1/26/10
to Pytho...@python.org
siddhartha veedaluru wrote:
> Hi,
>
> except not able to caught the TypeError exception occured in the below
> code
>
> log.info <http://log.info>("refer",ret) in the try block

>
> throws a TypeError which is not caught .

The traceback you provide does not show the line in question, so I am
confused as to why you would think it was causing the problem. The
purpose of the traceback is to show you where the exception occurred,
and I question whether that is happening here.

Anyway, I suspect your error might go away if you turned the first
argument of hte log.info() call into a format string such as

log.info("refer: %s", ret)

regards
Steve

> Also sometimes process is getting hanged.
>
> ------------------------------------------------------------------------------------------------------------------------------------------------
> import logging
> log = logging.getLogger()
> fileName = strftime("%d-%b-%Y-", gmtime()) + str(int(time.time())) + "-
> Log.log"
> log = logging.getLogger()
> log.setLevel(logging.NOTSET)
> fh = logging.FileHandler(logFile)
> logFileLevel = logging.DEBUG
> fh.setLevel(logFileLevel)
> format_string = '%(process)d %(thread)d %(asctime)-15s %(levelname)-5s
> at %(filename)-15s in %(funcName)-10s at line %(lineno)-3d "%(message)
> s"'
> fh.setFormatter(logging.Formatter(format_string))
> log.addHandler(fh)
>
> try:

> log.info <http://log.info>("start")
> log.info <http://log.info>("refer",ret)
> log.info <http://log.info>("end")


> except TypeError:
> log.exception("Exception raised")
>
> ----------------------------------------------------------------------------------------------------------------------------------------------
> OUTPUT message:
>
> Traceback (most recent call last):
> File "C:\Python26\lib\logging\__init__.py", line 768, in emit
> msg = self.format(record)
> File "C:\Python26\lib\logging\__init__.py", line 648, in format
> return fmt.format(record)
> File "C:\Python26\lib\logging\__init__.py", line 436, in format
> record.message = record.getMessage()
> File "C:\Python26\lib\logging\__init__.py", line 306, in getMessage
> msg = msg % self.args
> TypeError: not all arguments converted during string formatting
>


--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

Steve Holden

unread,
Jan 26, 2010, 11:54:23 AM1/26/10
to pytho...@python.org, Pytho...@python.org
0 new messages