473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Displaying error message in a try except?

Fairly new to python. In a try except how do you display the true
(raw) error message so it can be displayed back to the user?

Dec 11 '05 #1
2 1936
wa****@hotmail.com wrote:
Fairly new to python. In a try except how do you display the true
(raw) error message so it can be displayed back to the user?


assuming that "true" means "the message you would get if you hadn't
used a try/except", the traceback module is what you want:

import traceback

try:
raise SyntaxError("example")
except:
traceback.print_exc()

prints

Traceback (innermost last):
File "module.py", line 4, in ?
SyntaxError: example

more here:

http://effbot.org/librarybook/traceback.htm

you can also inspect the exception status via the sys.exc_info() call.
e.g.

import sys

try:
1/0
except:
print "%s: %s" % sys.exc_info()[:2]

prints

exceptions.ZeroDivisionError: integer division or modulo by zero

for more info, see the library reference.

hope this helps!

</F>

Dec 11 '05 #2
Fredrik Lundh wrote:
assuming that "true" means "the message you would get if you hadn't
used a try/except", the traceback module is what you want:

you can also inspect the exception status via the sys.exc_info() call.
e.g.

There is also the third way of catching an exception explicitly and
printing it's arguments and class (doesn't give exactly the same
information, but gives relevant informations nonetheless)

--
try:

1/0
except ZeroDivisionError, e:
print e
print e.args
print repr(e)
integer division or modulo by zero
('integer division or modulo by zero',)
<exceptions.ZeroDivisionError instance at 0x00E35850>
--

(catching Exception instead of ZeroDivisionError would yield the same
result, but would also act as an Exception trap that voids any exception
raised)
Dec 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Rahul Chatterjee | last post by:
Hello All I have an asp page in which I am performing the following 1. Querying a database view 2. Returning rows in to a recordset. 3. Looping thru the recordset and printing the data 4....
3
by: Jens Kristensen | last post by:
I have a problem displaying a divbox containing a html-textarea - everything works fine with "normal" characters. However, when the textarea contains special chars like <P> or ' , the box fails to...
3
by: Tim Graichen | last post by:
In subFormC, I would like to display only those records that have TrialDate and CompanyCode equal to TrialDate and CompanyCode in subFormB. Can somebody please help me? Thanks Tim
20
by: Peter E. Granger | last post by:
I'm having a strange problem (or at least it seems strange to me) trying to display a MessageBox in a VC++ .NET forms application. If I put the call to MessageBox::Show in the form's .h file, it...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
11
by: prats | last post by:
I want to write a GUI application in PYTHON using QT. This application is supposed to take in Japanese characters. I am using PyQt as the wrapper for using QT from python. I am able to take input...
20
by: Tim Reynolds | last post by:
Team, I am developing a web service. In testing in on my enw PC, I am expecting to see exceptions thrown appear on my browser. Instead I am getting an HTTP 500 Internal Server Error page and I am...
9
by: Jim | last post by:
Hello, I'm trying to write exception-handling code that is OK in the presence of unicode error messages. I seem to have gotten all mixed up and I'd appreciate any un-mixing that anyone can...
2
by: chazzy69 | last post by:
I having trouble displaying information i have retrieved from an mysql database, i succesfully execute the SELECT query as shown below- $sql = "SELECT PCODE,LOCALITY,STATE FROM `postcode` WHERE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.