473,394 Members | 2,168 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,394 software developers and data experts.

Catching error text like that shown in console?

I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback

Anyone know how I can do this or if it's possible? Didn't find anything doing a
search so I figured I'd hit up the experts.

Thanks.

-Pete Schott
Dec 9 '05 #1
3 1781

Peter A. Schott wrote:
I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback

Anyone know how I can do this or if it's possible? Didn't find anything doing a
search so I figured I'd hit up the experts.

Thanks.

-Pete Schott


I don't have an exact answer for you, but since no one else replied:

look at subprocess module, and os.popen4()

http://docs.python.org/lib/os-newstr...#os-newstreams
http://docs.python.org/lib/module-subprocess.html
http://mail.python.org/pipermail/pyt...er/305671.html

Hint: mentioning your O/S is pretty essential; for many issues, what
version of py and how you installed it also

Dec 9 '05 #2

Peter A. Schott wrote:
I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback

Anyone know how I can do this or if it's possible? Didn't find anything doing a
search so I figured I'd hit up the experts.

Thanks.

-Pete Schott


1. Create a function to be run when an exception happens.

def excepthook(self, type, value, tb):
#
# This function allows the user to redefine what happens if the program
# aborts due to an uncaught exception.
import traceback
#
# Get traceback lines and append the current session log
#
tblines=traceback.format_exception(type, value, tb)
#
# Print these lines somewhere. Normally you would log to a file
#
print "traceback exception encountered"
print "--------------------Begin traceback lines-------------------------"
print tblines
print "--------------------End traceback lines---------------------------")
map(sys.stdout.writelines, tblines) # Always write exceptions to screen
sys.exit(2)

2. Tell python you want to call this function when exception occurs.

sys.excepthook=exceptHandler
Dec 10 '05 #3
Peter A. Schott wrote:
I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback


If you just want to catch exceptions and print a traceback, use the
traceback module. This is handy for example if you are processing a
number of items and don't want a failure in one to abort the whole loop:

import traceback
for work in thingsToDo:
try:
doSomeWork(work)
except:
traceback.print_exc()

If you want more control over the exception info - for example to put it
in a string instead of printing it - look at the other functions in the
traceback module.

Kent
Dec 10 '05 #4

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

Similar topics

7
by: jennyw | last post by:
I'm trying to parse a product catalog written in HTML. Some of the information I need are attributes of tags (like the product name, which is in an anchor). Some (like product description) are...
16
by: Chris Maloof | last post by:
Hello, Does anyone know how I can read the ASCII text from a console window (from another application) in WinXP? It doesn't sound like a major operation, but although I can find the window via...
3
by: Michael G. Schneider | last post by:
I recently upgraded my servers to 2003. Since then the error-information shown in the browser has changed. For example, if under Windows 2000 I executed an SQL statement from within an ASP, and...
1
by: Nathan Baulch | last post by:
I have a page with EnableViewState="false". That page contains a DropDownList with dynamic items and a submit Button. I need to get the SelectedIndex of the DropDownList on postback however all...
1
by: Tosch | last post by:
I'm using a webclient to send data to a remote server. If an error exists in the data the remote server returns an http error 403 with an error text. I can find out that an error occured...
4
by: jawolter | last post by:
I have text that is too long to nicely fit on a given page, so I want to add ellipses to the end that dynamicaly resize based on the person resizing the page width. If you hover it would show the...
3
by: per9000 | last post by:
Hi, can I print some default text on the console when doing a ReadLine? A silly example below shows two small scenarios. In the first a user is told what he appears to be called, and asks for a...
0
by: Darrel | last post by:
Anyone know what would cause this error. It's not a typical error, it's text that is shown instead of what my usercontrol is supposed to render. ---------------------------- text/microsoft-resx...
1
by: autumnrrr | last post by:
th {height:50px;} td {padding:15px;} th {background-color:#0099cc;color:black;} .m {background-color:#7a52cc;color:black;} .t {background-color:#8a5ce6;color:black;} .b...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.