[Numpy-discussion] Question about improving genfromtxt errors
Skipper Seabold
jsseabold at gmail.com
Fri Sep 25 14:08:00 EDT 2009
On Fri, Sep 25, 2009 at 2:00 PM, Ralf Gommers
<ralf.gommers at googlemail.com> wrote:
>
>
> On Fri, Sep 25, 2009 at 1:00 PM, Skipper Seabold <jsseabold at gmail.com>
> wrote:
>>
>> There have been some recent attempts to improve the error reporting in
>> genfromtxt <http://projects.scipy.org/numpy/ticket/1212>, which is
>> great, because hunting down the problems reading in big and messy
>> files is not fun.
>>
>> I am working on a patch that keeps up with the line number and column
>> number of where you are in parsing the file, so that this can be
>> reported in the error. Is there a way to catch a raised error and add
>> to it?
>>
>> For instance, I have a problem in my file which leads to this error
>> being raised from np.lib._iotools.StringCoverter.upgrade
>>
>> ValueError: Converter is locked and cannot be upgraded
>>
>> I added this into np.lib.io.genfromtxt around line 995.
>>
>> linenum = 0
>> [...]
>> if dtype is None:
>> try:
>> colnum = 0
>> for (converter, item) in zip(converters, values):
>> converter.upgrade(item)
>> colnum += 1
>> except:
>> raise ValueError, "I don't report the error from
>> _iotools.StringConverter.upgrade, but I do know that there is a
>> problem trying to convert a value at line %s and column %s" %
>> (linenum,colnum)
>> [...]
>> linenum += 1
>>
>> I'd like to add line and column number information to original error
>> from _iotools. Any suggestions?
>
> There is no good way to edit the message of the original exception instance,
> as explained here:
> http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/
>
> Probably the easiest for your purpose is this:
>
> def divbyzero():
> return 1/0
>
> try:
> a = divbyzero()
> except ZeroDivisionError as err:
> print 'problem occurred at line X'
> raise err
>
> or if you want to catch any error:
>
> try:
> yourcode()
> except:
> print 'problem occurred at line X'
> raise
>
>
> Maybe better to use a logger instead of print, but you get the idea.
>
Ok thanks. Using a logger might be a good idea.
Skipper
More information about the NumPy-Discussion
mailing list