Consider this try/catch block I use for checking error message stored in e
.
Try/Catch to get the e
queryString = "SELECT * FROM benchmark WHERE NOC = 2"
try:
res = db.query(queryString)
except SQLiteError, e:
# `e` has the error info
print `e`
The e
object here contains nothing more than the above string. When python reports an unhandled error, however, it shows a pretty detailed info as below:
Traceback (most recent call last): File "fool.py", line 1, in open("abc.zyz", "r") IOError: [Errno 2] No such file or directory: 'abc.zyz'
My question is, how can I get the information such as above (the file and the line number etc.)? Or, if e
contains this info, how is it stored inside it?
Like the first 2 answers, use
traceback
. Here is a more complete example.When you run it, you'll see
This will show the trace to the error.
traceback
library.raise
within anexcept
block, which will then act like theexcept
block isn't there (aside from any conditional logic/side effects you may have done before theraise
).