Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Adding to Exception.args

Reply
Thread Tools

Adding to Exception.args

 
 
Andreas Beyer
Guest
Posts: n/a
 
      06-13-2005
Hi,

If an exception gets raised while I am parsing an input file I would
like to know where (in which line) the error occured. I do not want to
create my own exception class for that purpose and I also do not want to
deal with all possible kinds of exceptions that might occur.

I came up with the following code:

inp = file(file_name)
for n, line in enumerate(inp):
try:
# parse line ...
except Exception, e:
inp.close() # Is this necessary for 'r' files?
args = list(e.args)
args.insert(0, 'line: %d'%(n+1))
e.args = tuple(args)
raise

Which looks like this in the traceback:

ValueError: ('line: 3', 'invalid literal for float(): not_a_number')

Is this the 'correct' way to do that?
Is there any specific order to the arguments in e.args? Should my 'user
argument' be at the beginning or at the end of e.args?

Thanks! Andreas



 
Reply With Quote
 
 
 
 
Scott David Daniels
Guest
Posts: n/a
 
      06-13-2005
Andreas Beyer wrote:
> I came up with the following code:
> inp = file(file_name)
> for n, line in enumerate(inp):
> try:
> # parse line ...
> except Exception, e:
> inp.close() # Is this necessary for 'r' files?
> args = list(e.args)
> args.insert(0, 'line: %d'%(n+1))
> e.args = tuple(args)
> raise

inp = open(file_name) # GvR prefers this style, not file
try:
for n, line in enumerate(inp):
try:
# parse line ...
except Exception, e:
e.args = e.args + ('line: %d' % (n + 1),) # tuple add
raise
finally:
inp.close() # it is certainly more portable, and more readable


> Is there any specific order to the arguments in e.args?
> Should my 'user argument' be at the beginning or at the end of e.args?


If you are going to play with it, it is more likely that indices
are used (so making e.args[0] refer to the same thing may help).
The __str__ method of the Exception is responsible for formatting.
If you "wrap" an exception that has its own __str__ method, it
may well expect the args tuple to hold a precise number of
elements (and therefore fail to convert to a string).

--Scott David Daniels

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Adding Wireless to a Hard-Wired Home Office (w/Web & Email) Nick Knight Wireless Networking 2 01-06-2005 03:46 AM
Adding an Antenna Pigtail to a WiFi card Guy Smith Wireless Networking 0 12-31-2004 11:15 AM
Adding Linksys or Dlink into a Microsoft networking product config =?Utf-8?B?c2tsZWZmbg==?= Wireless Networking 0 12-03-2004 04:12 PM
RE: Adding laptop to a new network =?Utf-8?B?TWFyayBQ?= Wireless Networking 0 10-20-2004 07:45 PM
Adding Wireless acess point to existing network. Shawn Wireless Networking 1 10-17-2004 03:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57