TypeError not caught by except statement

S

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)
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
 
A

Alf P. Steinbach

* 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
 
C

Carl Banks

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top