Awsome Python - chained exceptions
steve+comp.lang.python at pearwood.info
Thu Feb 14 07:39:19 CET 2013
On Thu, 14 Feb 2013 09:10:42 +1100, Chris Angelico wrote:
Quoting Rick Johnson:
>> Q2: Why would the line in the try block be shown as a "feature" of
>> the traceback when the whole intent of exception handling is to hide
>> the error in the try block! If you want to raise the exception in the
>> try block then you place a naked raise statement in the exception
>> block, BUT THEN, why even wrap the code in a try/except in the first
>> damn place!?
Here is one example of using raise to re-raise an exception you have just
paths = ["here", "there", "somewhere else"]
for location in paths:
filename = os.path.join(location, "prefs.ini")
f = open(filename)
except IOError as e:
if e.errno != errno.ENOENT: # File not found.
> You seriously need to get into the real world and do some actual
> debugging work.
Amen to that brother. What is it that they say?
"Those who can, do. Those who can't, teach. Those who can't teach, write
rants on the Internet criticising others."
>>> He is? Could just as easily be the print statement with a single
>>> argument, with unnecessary parentheses around that argument. Which, if
>>> I recall correctly, is one of the recommended approaches for making
>>> 2/3 bi-compatible code.
>> Because if he did in-fact write the print statement using parenthesis
>> (in some foolish attempt to make his code forward-compatible) that
>> would mean i should add /another/ coding abomination to my earlier list
>> of abominations. The proper method of using a forward compatible print
>> function is by /importing/ the feature.
>> from future import print_function
>>>> import __future__
> _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 65536)
> Which works back as far as 2.6 but that's all. Simply putting parens
> around the argument works all the way back to... hmm. Further back than
> I've ever had to support, but then, I only started using Python
> seriously a few years ago. Steven?
Putting parens around the argument to print works going back to at least
Python 0.9.1, which is before Python had "" delimiters:
steve at runes:~$ ./python0.9.1
>>> s = "string"
Parsing error: file <stdin>, line 1:
s = "string"
Unhandled exception: run-time error: syntax error
>>> s = 'string'
>>> print s
>>> print (s)
You can always wrap any expression with an extra pair of round brackets.
Of course, the correct way of doing this is with "from __future__ import
print_function", but really, who cares? It's just a trivial example. If
the worst criticism someone can make of my example is that I took a short-
cut when printing, I can live with that.
More information about the Python-list