473,703 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding to Exception.args


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):
# 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)

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

Jul 19 '05 #1
1 2285
Andreas Beyer wrote:
I came up with the following code:
inp = file(file_name)
for n, line in enumerate(inp):
# 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
for n, line in enumerate(inp):
# parse line ...
except Exception, e:
e.args = e.args + ('line: %d' % (n + 1),) # tuple add
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
Sc***********@A cm.Org
Jul 19 '05 #2

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

Similar topics

by: Thomas Weholt | last post by:
Hi, I want to add methods to a class instance at run-time based on a base method, but modify one of the existing ( or add if possible ) params of the base method. Say a class like this : class A: def __init__(self): self.obj1() # some dummy object having a name attribute
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .... py> C().spam(3) spam spam spam
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library. When i try to connect to a server that doesnt exist it raises a "Connection forcibly rejected by the resmote host" SocketException.
by: Aren Cambre | last post by:
Why does SmtpMail.Send throw an exception if the MailMessage's BodyFormat = MailFormat.Html? I've searched all over the place and cannot find a solution anywhere. I am running this on Windows XP SP2, and IIS's SMTP and WWW servers are installed. Here is the error trace: System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an...
by: jim4u | last post by:
Hi gurus, I have an automation add-in created using C# for Excel, in which I am exposing a number of functions. Is there any way I can add help-text the way excel does for other categories like Financial, Statistical, etc. The place I want my help-text to be available is in the Insert->Function. Here, we can select a category and function. The corresponding help text is displayed. Now, my automation add-in is
by: moondaddy | last post by:
I'm running a wpf windows app and want to add the appSettings element to the config file. when I do, the app wont start up and I get the following error: {"The type initializer for 'System.Windows.Application' threw an exception."} {"The type initializer for 'System.Windows.Navigation.BaseUriHelper' threw an exception."} " at System.Windows.Application..ctor()\r\n at DiagramTool_v1.App..ctor()\r\n at DiagramTool_v1.App.Main() in
by: Russ P. | last post by:
I am baffled about why my exception messages are not displaying properly. I have a class that represents physical scalars with units. If I type I should get something like this: scalar.InconsistentUnits: 3 s, 4 m
by: Joel Koltner | last post by:
I have a generic (do nothing) exception class that's coded like this: class MyError(exceptions.Exception): def __init__(self,args=None): self.args = args When I attempt to raise this exception via 'raise MyError' I get an exception within the MyError constructor __init__ as follows: Traceback (most recent call last):
by: Scott | last post by:
I'm debugging an xmlrpc client/server application. Often when an exception occurs in the server, I receive only a very short error message on the client. For example: xmlrpclib.Fault: <Fault 1: "<type 'exceptions.AssertionError'>:"> Presumably this is because xmlrpclib on the server is catching the exception, and only sending the exception name to the client, not the server's stack trace.
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.