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

forcing exceptions

Is there a way to tell the interpreter to display exceptions, even those
which were captured with except?

--
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
Mar 3 '06 #1
5 1269
Nikola Skoric <ni*******@net4u.hr> writes:
Is there a way to tell the interpreter to display exceptions, even those
which were captured with except?


Normally you wouldn't do that unless you were trying to debug the
interpreter itself. It uses caught exceptions for all sorts of things
that you probably don't want displayed. I think even ordinary loop
termination may be implemented using exceptions.
Mar 3 '06 #2
Nikola Skoric wrote:
Is there a way to tell the interpreter to display exceptions, even those
which were captured with except?

Yes, sort of ... You have to trigger the display yourself within the
capturing "except" -- it's not automatic.

The traceback module provides a number of functions that can be used to
display a traceback of the current (or any other) exception. In
addition, it can produce a traceback of the current state of the
execution stack even without an exception:

Example (type this into the Python interpreter):

import traceback
try:
assert False
except:
traceback.print_exc()

and you'll get a traceback (a very short one in this case):

Traceback (most recent call last):
File "<stdin>", line 2, in ?
AssertionError

Gary Herron
Mar 3 '06 #3
In article <7x************@ruckus.brouhaha.com>, Paul Rubin
<http://ph****@NOSPAM.invalid> says...
Nikola Skoric <ni*******@net4u.hr> writes:
Is there a way to tell the interpreter to display exceptions, even those
which were captured with except?


Normally you wouldn't do that unless you were trying to debug the
interpreter itself. It uses caught exceptions for all sorts of things
that you probably don't want displayed. I think even ordinary loop
termination may be implemented using exceptions.


Yes, thanks for your quick responses, all three. You're right, I don't
want to debug python :-) But I figured out that I don't need captured
exceptions, the thing is that I just didn't belive the problem was that
obvious. In fact, problem was in the except block, not in it's try
block. The except block had this inocent statement:

print self.sect[1].encode('utf-8')

Which results in:

Traceback (most recent call last):
File "AIDbot2.py", line 238, in ?
bot.checkNominations()
File "AIDbot2.py", line 201, in checkNominations
if sect.parseSect() == 1:
File "AIDbot2.py", line 96, in parseSect
print self.sect[1].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 15:
ordinal
not in range(128)

Now, who can it complain about 'ascii' when I said loud and clear I want
it to encode the string to 'utf-8'??? Damn unicode.

--
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
Mar 3 '06 #4
Nikola Skoric wrote:
print self.sect[1].encode('utf-8')

Which results in:

Traceback (most recent call last):
File*"AIDbot2.py",*line*238,*in*?
bot.checkNominations()
File*"AIDbot2.py",*line*201,*in*checkNominations
if*sect.parseSect()*==*1:
File*"AIDbot2.py",*line*96,*in*parseSect
print*self.sect[1].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 15:
ordinal
not*in*range(128)

Now, who can it complain about 'ascii' when I said loud and clear I want
it to encode the string to 'utf-8'??? Damn unicode.


Trying to encode a str s results in something like

unicode(s, "ascii").encode("utf-8")

where the first step, i. e. the conversion to unicode fails for non-ascii
strings. The solution is to make that conversion explicit and provide the
proper encoding. Assuming the initial string is in latin1:

unicode(self.sect[1], "latin1").encode("utf-8")

Of course, if you can ensure that self.sect[1] is a unicode instance earlier
in your code, that would be preferable.

Peter
Mar 3 '06 #5
Nikola Skoric wrote:
Traceback (most recent call last):
File "AIDbot2.py", line 238, in ?
bot.checkNominations()
File "AIDbot2.py", line 201, in checkNominations
if sect.parseSect() == 1:
File "AIDbot2.py", line 96, in parseSect
print self.sect[1].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 15:
ordinal
not in range(128)

Now, who can it complain about 'ascii' when I said loud and clear I want
it to encode the string to 'utf-8'??? Damn unicode.


Presumably, self.sect[1] is not a unicode string, but a regular string. The
utf-8 encoder only takes unicode strings, so it tries to convert the input into
a unicode string first. ASCII is the default encoding when no encoding is specified.

Standard practice with unicode is to only do conversions at the boundaries
between your code and interfaces that really need bytes (files, the console, the
network, etc.). Convert to unicode strings as soon you get the data and only
convert to regular strings when you have to. Everything *inside* your code
should be unicode strings, and you shouldn't try to pass around encoded regular
strings if at all possible, although pure ASCII strings generally work because
it is the default encoding.

So double-check self.sect[1] and figure out why it isn't a unicode string.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Mar 3 '06 #6

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

Similar topics

16
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem...
12
by: Ritz, Bruno | last post by:
hi in java i found that when a method has a throws clause in the definition, callers must either handle the exceptions thrown by the method they are calling or "forward" the exception to the...
17
by: Bryan Bullard | last post by:
hi, is there a way to force the user of an object to catch exceptions of specific type? for instance a compile error is issued if exception 'ExeceptionXXX' thrown by method...
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
2
by: AC [MVP MCMS] | last post by:
Having a heck of a time trying to create a ton of AD user accounts in a specific OU without having the users be forced to change their password upon a successful login. After creating the...
9
by: Joel Byrd | last post by:
I've got a div whose width is specified as a percentage so that if you shrink the browser window, the div shrinks, and the text inside the div wraps around to accommadate this. The problem is:...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
2
by: Steve | last post by:
Hi, Is their any way to setup VS such that it forces programmers to handle all exception (similar to Java). Thanks --
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
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: 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...
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
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
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...
0
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,...

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.