473,545 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with trapping an exception

I have a piece of code like this:

try:
some code
except:
print >> sys.stderr, "error: ", sys.exc_info()
When an exception is thrown from the code, what I see on the console is this:

error: (<class exceptions.Name Error at 0x01E7A378>, <exceptions.Nam eError
instance at 0x09752C50>, <traceback object at 0x096A1408>)

I had a lot of trouble figuring out what was causing the NameError. I finally
figured it out by removing the "try" and "except" statements so that my code
didn't catch any exceptions. When I did that, I saw the following on my
console:

Traceback (most recent call last):
File "c:\Documen ts and Settings\xxxxx\ rot.PspScript", line 79, in Do
os.path.walk(ba seDirectory, ProcessDirector y, Environment)
File "c:\Program Files\xxxxxx\Py thon Libraries\lib\n tpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documen ts and Settings\xxxxx\ rot.PspScript", line 36, in
ProcessDirector y
App.Do(Environm ent, 'FileOpen', {
NameError: global name 'Environment' is not defined
Now that was much more helpful. Once I knew that 'Environment' was not
defined, I easily figured out the problem. But I need to use "try" and
"except" to catch the exception, otherwise my program will end abruptly
without finishing. So how can I see the "NameError: global name 'Environment'
is not defined" message in the "except" section of my code? A call to
sys.exc_info() doesn't show it. Is there another function I can use? Thanks.

Jul 18 '05 #1
3 1931
Take a look at the traceback standard module to format exceptions
nicely. Another thing you can do is this:

try:
{}['fu']
except Exception, x:
print x

That asigns the exception to x in the except block. All exceptions can
be converted to strings which are usually short descriptions of the
problem. One problem is KeyError, which converts to just the bad key,
for example the above just prints "fu" which is less than helpful.
Another catch of the above is that it will only catch subclasses of
Exception, which is almost everything except a few like StopIteration or
user classes that don't subclass Exception.

On Mon, Aug 23, 2004 at 09:05:15PM -0400, py****@bag.pyth on.org wrote:
I have a piece of code like this:

try:
some code
except:
print >> sys.stderr, "error: ", sys.exc_info()
When an exception is thrown from the code, what I see on the console is this:

error: (<class exceptions.Name Error at 0x01E7A378>, <exceptions.Nam eError
instance at 0x09752C50>, <traceback object at 0x096A1408>)

I had a lot of trouble figuring out what was causing the NameError. I finally
figured it out by removing the "try" and "except" statements so that my code
didn't catch any exceptions. When I did that, I saw the following on my
console:

Traceback (most recent call last):
File "c:\Documen ts and Settings\xxxxx\ rot.PspScript", line 79, in Do
os.path.walk(ba seDirectory, ProcessDirector y, Environment)
File "c:\Program Files\xxxxxx\Py thon Libraries\lib\n tpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documen ts and Settings\xxxxx\ rot.PspScript", line 36, in
ProcessDirector y
App.Do(Environm ent, 'FileOpen', {
NameError: global name 'Environment' is not defined
Now that was much more helpful. Once I knew that 'Environment' was not
defined, I easily figured out the problem. But I need to use "try" and
"except" to catch the exception, otherwise my program will end abruptly
without finishing. So how can I see the "NameError: global name 'Environment'
is not defined" message in the "except" section of my code? A call to
sys.exc_info() doesn't show it. Is there another function I can use? Thanks.

Jul 18 '05 #2
On Mon, 23 Aug 2004 21:13:48 -0400, Phil Frost <in****@bitglue .com> wrote:
Take a look at the traceback standard module to format exceptions
nicely.


Ah, this looks like what I want. Although when I use either
traceback.print _tb(sys.exc_inf o()[2]) or traceback.print _exc(), it only shows
the most recent call in the traceback. This is what it shows:

Traceback (most recent call last):
File "C:\Documen ts and Settings\xxxxx\ rot.PspScript", line 36, in
ProcessDirector y
App.Do(Environm ent, 'FileOpen', {
Why doesn't it show everything, which would be:

Traceback (most recent call last):
File "c:\Documen ts and Settings\xxxxx\ rot.PspScript", line 79, in Do
os.path.walk(ba seDirectory, ProcessDirector y, Environment)
File "c:\Program Files\xxxxxx\Py thon Libraries\lib\n tpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documen ts and Settings\xxxxx\ rot.PspScript", line 36, in
ProcessDirector y
App.Do(Environm ent, 'FileOpen', {
NameError: global name 'Environment' is not defined

Jul 18 '05 #3
pythos wrote:
not defined, I easily figured out the problem. But I need to use
"try" and "except" to catch the exception, otherwise my program will
end abruptly without finishing. So how can I see the "NameError:
global name 'Environment' is not defined" message in the "except"
section of my code? A call to sys.exc_info() doesn't show it. Is
there another function I can use? Thanks.


I suggest using this code from the Python cookbook:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52215

I also strongly recommend getting the dead tree version as
well. That recipe and many others are in it, and it is all
well laid out with lots of good discussion.

Roger
Jul 18 '05 #4

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

Similar topics

3
661
by: JP | last post by:
I need to be able to trap errors at the application level. I added this code to the Global.asax file. The code I wrote is supposed to get the last error that was generated and write to the event log as well as fire up my email class and generate an email to send me the error. But no matter what I do, I still get the icky looking yellow and...
19
2158
by: KKramsch | last post by:
One of the features from other languages that I miss most in C is trappable exceptions. More specifically, I think it's great to be able to demarcate a whole block of code where several exceptions can happen at various points, so that any one of these exceptions can be trapped and handled by the exception-handling code collected in one place...
4
2086
by: Bill | last post by:
Despite our best efforts occasionally in an aspx file, something like <%=x%> where x is not defined sqeaks by and I get the ugly asp error message. I want to be able to identify this particular error and issue a pretty message. I use the global.asax on application error to handle generalized error handling. I want to be able to capture and...
1
1067
by: SMG | last post by:
Hi All, I am using following code in web.confiig for trapping all the error through out my site.. <customErrors mode="On" defaultRedirect="WebForm1.aspx"> <error statusCode="404" redirect="ServerError.aspx"></error> <error statusCode="500" redirect="WebForm3.aspx"></error> </customErrors>
6
1466
by: SMG | last post by:
Hi , Sory for incomplete message in last post here is the actual problem.. I am using following code in web.confiig for trapping all the error through out my site.. <customErrors mode="On" defaultRedirect="WebForm1.aspx"> <error statusCode="404" redirect="ServerError.aspx"></error> <error statusCode="500"...
6
1563
by: TechieMom | last post by:
I have the following code: Dim oFile As Stream Dim oReader As StreamReader Dim strCarName oFile = .GetExecutingAssembly.GetManifestResourceStream("Assignment3.Vehicles.txt")
0
271
by: Peter Newman | last post by:
vb.net 2003 Sql 2005 Ive been banging my head against a brick wall for the last day trying to figure out what im doing wrong I have a Dataset and a Form with text boxes that are bound to the dataset. If i edit a record how do i trap for the Dataset.HasChanges and update the datsbase as im doing it when i move from one record to the...
9
2096
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR | E_PARSE); set_error_handler("SendErrorReport"); function SendErrorReport($errorNumber, $errorMessage, $errorFile, $errorLine, $vars) {
5
1471
by: Yash | last post by:
Hi, I have create a user control which has a dropdown. The user control internaly handles the Selected_Index_Changed event of the dropdown. If there is an error/exception in handling it, I would like the containing page to know of it. How can I make this possible? The containing page does not know of the dropdown. Thanks, Yash
0
7692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.