help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to make "division by zero attempted" error message more informat


From: Neil R. Ormos
Subject: Re: How to make "division by zero attempted" error message more informative?
Date: Thu, 3 Mar 2022 13:55:23 -0600 (CST)

Peng Yu wrote:
> [Neil R. Ormos wrote:]
>> Peng Yu wrote:

>>> I see something like this. But when this kind
>>> of command is called at multiple scripts. It
>>> is hard to tell where the error is from.

>>> $ awk -e '{ print 10/$1 }' <<< 0
>>> awk: cmd. line:1: (FILENAME=- FNR=1) fatal: division by zero attempted

>>> Is there a way to customize the error message
>>> so that I can know where the error is coming
>>> from? Thanks.

>> One way to address this:

>> Gawk error messages are prefaced with the name
>> by which gawk was called.

>> For each call to gawk in the multiple scripts,
>> you could create a symlink to the gawk
>> executable with a different distinguishing
>> name, and then call gawk using that name.
>> Then, each error message will be prefaced by
>> the distinguishing name. [...]

> This is not a viable solution. 

> I have awk called
> in my scripts. I make no sense to change the awk
> names in those scripts.

| That which we call a rose
| By any other name would smell as sweet;

Par for the course:  a secret constraint newly disclosed only after a solution 
is offered. 

There is no magic way for Gawk to know what should be printed in the error 
message to distinguish a particular call.

Whether you use the technique I suggested, or the technique Ed Morton 
suggested, or the Gawk maintainers choose to add a feature that lets you insert 
some arbitrary string in an error message, there has to be some means to 
distinguishably identify the particular call instance and communicate that to 
gawk.

In the reductive example you offered, where the Gawk program text is embedded 
in the calling shell script, there's no getting around editing every shell 
script.  I like Ed's technique for that.

If the real use case involves Gawk programs stored external to the shell 
script, then you'd still have to edit at least one of (a) all the calling shell 
scripts; or (b) all the Gawk programs.  

An advantage of the technique I proposed is that it can unambiguously identify 
both the calling shell script and the called Gawk program from which the error 
message was printed, without changing any of the Gawk programs, at the expense 
of the mild ugliness of the symlink infrastructure, which could be reused for 
non-overlapping suites of Gawk programs.

For a big enough project, there's probably an interesting chicane that could 
exploit Gawk's internationalization features by defining artificial languages 
and providing the bespoke error messages as though they were translations of 
the ordinary error messages into a selected artificial language.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]