18

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.

1
  • 1
    Maybe we can help you better if you give us the stack trace output and the actual code. Commented Jun 18, 2013 at 20:24

4 Answers 4

22

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

3

Try using:

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

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

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.