Bill Allombert on Sat, 27 Nov 2010 01:09:24 +0100


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

Re: Question regarding error()


On Fri, Nov 26, 2010 at 02:28:46PM -0500, Jeff wrote:
> PARI users,
> 
> Iâve started using PARI for class, and although I like it, it has its 
> quirks.  Can you help with this?

Hello Jeff,

To start with, which version of PARI/GP are you using ?
Have you considered using trap(,,) for error handling ?

> In a file:

How do you read the file ?

> errmsg = ""  \\ last error message
> errno  = 0   \\ last error indicator
> 
> errget() = error( errmsg )  \\ Problem with this - once error() is called, 
> errno and errmsg are kill()ed.

Technically they are not killed. Instead, when an error occurs, the interpretor 
is reset to the state at the last prompt.
In any case, error() does not return, so I am not sure how you planned to use
the value of errmsg/errno after errget() is called.

> errset( msg ) =
> {
>     errmsg = concat( "        ", msg );
>     errno  = 1;
> }

Better use Str("        ", msg );

> ...
> 
> In another file:
> 
> LJ(m,n) =
> {
>     if( n < 1  ||  !bitand(n,1),
>         errset( "LJ(m,n): 2nd parameter must be a positive odd integer." );
>         errget();
>         return()
>     );
> ... this function continues
> 
> So, you might be able to tell that Iâm trying to implement some rudimentary 
> error handling.

Honestly I do not see how your error-handling code is supposed to be used.

>  Letâs say I comment out errget() in LJ().  errset() does 
> what I expect, and if I call errget() from the GP shell it does what I 
> expect.  Now letâs uncomment errget() in LJ().  It seems to work correctly, 
> but then errno and errmsg are toast and have type t_POL.  Huh?  Just how 
> does that make any sense?

Actually errno and errmsg initial values have type t_POL (as for any GP variable), so when 
their values are reset, they become t_POL again.

>  Oh, and hereâs something else, one of the 
> "quirks" I mentioned above: in LJ() I once called errset( "\n        " 
> "LJ(m..." but moved the "\n        " to concat() in errset(), but I had to 
> get rid of the \n to get the same effect.  It seems an implicit newline was 
> inserted.  I've seen this elsewhere while using print1()s, and that's 
> baffling.

The only case I am aware of where \n is inserted is before printing the prompt,
to make sure it is at the start of the line.

Cheers,
Bill.