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

question about sys.exc_type and sys.exc_value

Here is an exercise from Learning Python that I wrote myself:

import sys
import traceback

class MyError: pass

def oops():
raise MyError()

def safe(func, *args):
try:
apply(func, args)
except:
traceback.print_exc()
print 'Got', sys.exc_type, sys.exc_value

safe(oops)

And here is the output:

Traceback (most recent call last):
File "C:/Python24/oops.py", line 11, in safe
apply(func, args)
File "C:/Python24/oops.py", line 7, in oops
raise MyError()
MyError: <__main__.MyError instance at 0x00B475A8>
Got Queue.Empty

Why does it show Queue.Empty as the values for sys.exc_type and
sys.exc_value? I guess I can somewhat understand Empty, because the
instance is basically nothing. But why does it say Queue instead of
MyError? The sample in the book shows "Got hello world", because hello
is the error name and world is the extra data. But mine doesn't seem to
work that way. (They are also using a string exception in the book,
instead of a class.)

Thanks.
Mar 5 '06 #1
3 2823
John Salerno wrote:
MyError? The sample in the book shows "Got hello world", because hello
is the error name and world is the extra data. But mine doesn't seem to
work that way. (They are also using a string exception in the book,
instead of a class.)


My mistake. In the book the exception is:

MyError = 'hello'

and the extra data is 'world'
Mar 5 '06 #2
John Salerno wrote:
Here is an exercise from Learning Python that I wrote myself:

import sys
import traceback

class MyError: pass

def oops():
raise MyError()

def safe(func, *args):
try:
apply(func, args)
except:
traceback.print_exc()
print 'Got', sys.exc_type, sys.exc_value

safe(oops)

And here is the output:

Traceback (most recent call last):
File "C:/Python24/oops.py", line 11, in safe
apply(func, args)
File "C:/Python24/oops.py", line 7, in oops
raise MyError()
MyError: <__main__.MyError instance at 0x00B475A8>
Got Queue.Empty

Why does it show Queue.Empty as the values for sys.exc_type and
sys.exc_value? I guess I can somewhat understand Empty, because the
instance is basically nothing. But why does it say Queue instead of
MyError? The sample in the book shows "Got hello world", because hello
is the error name and world is the extra data. But mine doesn't seem to
work that way. (They are also using a string exception in the book,
instead of a class.)

Thanks.


Are you using the first edition of "Learning Python"? According to
<http://www.oreilly.com/catalog/lpython/> that was published in 1999,
which puts it right around the end of Python 1.5. I'd strongly suggest
you find a much more recent tutorial, as a lot of python has been
improved and altered since then (sometimes in incompatible ways).

If you look at the sys module's documentation at
<http://docs.python.org/lib/module-sys.html>, you'll notice that
sys.exc_{type,value,traceback} have been deprecated since version 1.5.
That's because there's really no way to use them safely. The reason is
that as global variables, they hold information on the most recent
exception handled _anywhere_ in the program, possibly in another
thread. You should use sys.exc_info() instead, which only returns the
most recent one in the current thread.

If you do a little experimentation, you should notice that your
anomalous result only occurs when you run the code through IDLE -- it
works fine at the command prompt. Also, if you print exc_type and
exc_value first, at the beginning of the except block, it works fine.
The reason for this behavior is that IDLE runs your code in a separate
thread of the same interpreter instance used to run IDLE itself.
Presumably, some code in IDLE's user interface uses a queue to manage
the display, and that results in a Queue.Empty exception being thrown
and caught internally in another thread whenever a line of output is
printed.

The point is that sys.exc_type, sys.exc_value and sys.exc_traceback are
fundamentally unsafe, which is why they've been deprecated for the last
7 years. Incidentally, string exceptions are also deprecated IIRC, so
it's probably best to steer clear of them as well.

Hope this helps,
-- David

Mar 5 '06 #3
David Wahler wrote:
Are you using the first edition of "Learning Python"? According to
<http://www.oreilly.com/catalog/lpython/> that was published in 1999,
which puts it right around the end of Python 1.5. I'd strongly suggest
you find a much more recent tutorial, as a lot of python has been
improved and altered since then (sometimes in incompatible ways).


Actually I'm using the second edition, which is updated to 2.2/2.3. But
you are right, I just tried it in the command prompt and it does work,
so my mind can rest tonight. :)
Mar 5 '06 #4

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

Similar topics

0
by: Jason Kratz | last post by:
After figuring out the other day (thanks to everyone here who helped) how to get the dir lists I want on my unix machine I want to turn the same thing into an XML-RPC service. Using Simple...
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
8
by: kbperry | last post by:
In Python, When using the default except (like following) try: some code that might blow up except: print "some error message"
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
6
by: DarkBlue | last post by:
My application makes several connections to a remote database server via tcp/ip. Usually all is fine,but occasionally the server is down or the internet does not work and then there is the 30 sec...
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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...

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.