Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 377
avorum Avatar asked Jun 18 '13 20:06

avorum


People also ask

How do I get an exception message in Python?

The most common method to catch and print the exception message in Python is by using except and try statement. You can also save its error message using this method.

What is Ioerror in Python?

It is an error raised when an input/output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. It is also raised for operating system-related errors.

What is a TypeError in Python?

The Python TypeError is an exception that occurs when the data type of an object in an operation is inappropriate. This can happen when an operation is performed on an object of an incorrect type, or it is not supported for the object.


1 Answers

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.

like image 149
Martijn Pieters Avatar answered Oct 02 '22 16:10

Martijn Pieters