avorum
avorum

Reputation: 2293

empty error message in python

I'm trying to debug an error, I got a "no exception supplied" when I ran it initially and then later put in a try/except block to print out whatever the error was.

try:
    #some code
except BaseException, e:
    print str(e)

This produces a blank line of output, any ideas what it could be?

EDIT: Sorry, was hoping there was a specific reason that the error message could be blank. There is no stack trace output which is what caused me to be forced to do a try/except block in first place, I'm still programming this thing so I'm just letting the 'compiler' catch the errors for now. The actual code that's throwing the error is in a Django app so it'll have some functions from Django.

try:
    if len(request.POST['dateToRun']) <= 0:
        dateToRun = Job.objects.filter(id=jobIDs[i]).values()['whenToRun'].split(' ')[0]
    if len(request.POST['timeToRun']) <= 0:
        timeToRun = Job.objects.filter(id=jobIDs[i]).values()['whenToRun'].split(' ')[1]
except BaseException, e:
    print str(e)

This is code in a view function. jobIDs is a dict containing value key pairs in the format ##Selection: ## (ie 17Selection: 17). Sorry I forgot to post this initially.

EDIT: repr(e) has given me TypeError() which is better than not knowing anything.

Upvotes: 18

Views: 26321

Answers (4)

D4zk1tty
D4zk1tty

Reputation: 133

Try using:

try:
    #code
except BaseException as e:
    print str(e)

This seems to be the easiest to understand, and affective.

Upvotes: 3

Martijn Pieters
Martijn Pieters

Reputation: 1122312

This means the exception has no message attached. Print the exception type:

print repr(e)

You may also want to print the traceback:

import traceback

# ...
except BaseException as e:
    traceback.print_exc()

You want to avoid catching BaseException however, this is no better than a blanket except: statement. Catch more specific exceptions instead.

Upvotes: 22

Chris Barker
Chris Barker

Reputation: 2389

Put the try/except block around smaller sections of code until you find the offending line. For example, if you have:

try:
    a = fn(b)
    c = fn(a)
except BaseException, e:
    print str(e)

Then change it to:

a = fn(b)
try:
    c = fn(a)
except BaseException, e:
    print str(e)

Upvotes: 1

Andrew Clark
Andrew Clark

Reputation: 208495

The following produces a blank line of output:

try:
    raise Exception()
except BaseException, e:
    print str(e)

Use repr(e) to see what the exception is that was raised.

Upvotes: 7

Related Questions