473,395 Members | 1,403 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,395 software developers and data experts.

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.NameError at 0x01E7A378>, <exceptions.NameError
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:\Documents and Settings\xxxxx\rot.PspScript", line 79, in Do
os.path.walk(baseDirectory, ProcessDirectory, Environment)
File "c:\Program Files\xxxxxx\Python Libraries\lib\ntpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 36, in
ProcessDirectory
App.Do(Environment, '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 1920
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.python.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.NameError at 0x01E7A378>, <exceptions.NameError
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:\Documents and Settings\xxxxx\rot.PspScript", line 79, in Do
os.path.walk(baseDirectory, ProcessDirectory, Environment)
File "c:\Program Files\xxxxxx\Python Libraries\lib\ntpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 36, in
ProcessDirectory
App.Do(Environment, '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_info()[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:\Documents and Settings\xxxxx\rot.PspScript", line 36, in
ProcessDirectory
App.Do(Environment, 'FileOpen', {
Why doesn't it show everything, which would be:

Traceback (most recent call last):
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 79, in Do
os.path.walk(baseDirectory, ProcessDirectory, Environment)
File "c:\Program Files\xxxxxx\Python Libraries\lib\ntpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 36, in
ProcessDirectory
App.Do(Environment, '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
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...
19
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...
4
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...
1
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"...
6
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"...
6
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
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...
9
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 |...
5
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.