[R] Graceful exit from fortran.

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 1 16:47:48 CEST 2001


On Mon, 1 Oct 2001, Rolf Turner wrote:

>
> Is there a way to exit gracefully from dynamically loaded Fortran,
> (several layers down), if an error condition is detected?
>
> I.e. suppose I'm within a subroutine called by a subroutine, ...,
> called by .Fortran(); I want to give up gracefully if an error
> condition is detected.
>
> If I say something like
>
>       if(x .gt. 42.d0) stop
>
> then indeed everything stops, i.e. R falls over.  I'd ***like*** to
> be able to print out an informative error message (which I guess
> could be done --- a wee bit kludgily --- by calling, say, intpr())
> and then exit gracefully, with R continuing to run.
>
> Is this possible, or is it just too much too expect?

You can borrow the idea of the following piece of code in src/main/print.c

/* Fortran-callable error routine for lapack */

void F77_NAME(xerbla)(char *srname, int *info)
{
    error("On entry to %6s parameter number %d had an illegal value",
          srname, *info);
}

which although I am not sure should always work (passing Fortran strings
being the problem), no one has complained.

A safer route is to add fexitc.c and fexitf.f to your compile, these being
something like

      subroutine fexit(msg)
      int nc
      character*(*) msg
      nc = len(msg)
      call fexitc(msg, nc)
      end

#include <R.h>

void F77_NAME(fexitc)(char *msg, int *nchar)
{
    int nc = *nchar;
    char buf[256];
    if(nc > 255) {
        warning("invalid character length in fexitc");
	nc = 255;
    }
    strncpy(buf, msg, nc);
    buf[nc] = '\0';
    error(buf);
}


Then use

      call fexit('My error message')


I'll add something like that for R 1.4.0.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list