Error functions — CALCEPH - Fortran 77/90/95 language 3.4.1 documentation

# Error functions

The following group of functions defines the behavior of the library when errors occur during the execution.

## Usage

The following examples, that can be found in the directory examples of the library sources, show the typical usage of this group of functions.

The example in Fortran 77/90/95 language is `f77error.f`.

The following example shows how to stop the execution on the error.

    program f77error
        implicit none
        include 'f90calceph.h'
        integer*8 peph
        integer res

        ! set the  error handler to stop on error
         call f90calceph_seterrorhandler(2, 0)

        ! open the ephemeris file
        res = f90calceph_open(peph, "example1.dat")
        ! ...
    stop
    end

The following example shows how to define a custom error handler function.

     !-----------------------------------------------------------------
     ! custom error handler
     !-----------------------------------------------------------------
             subroutine myhandler(msg)
                implicit none
                character(len=*) :: msg
                write (*,*) "The calceph calls the function myhandler"
                write (*,*) "The message contains ",len(msg)," characters"
                write(*,*) "The error message is :"
                write(*,*) "----------------------"
                write(*,*) msg
                write(*,*) "----------------------"
                write(*,*) "The error handler returns"
             end

     !-----------------------------------------------------------------
     ! main program
     !-----------------------------------------------------------------
     program f77error
        implicit none
        include 'f90calceph.h'
        integer res
        integer*8 peph
        external myhandler

         ! set the  error handler to use my own callback
         call f90calceph_seterrorhandler(3, myhandler)

        ! open the ephemeris file
        res = f90calceph_open(peph, "example1.dat")
        ! ...
    stop
    end

## f90calceph_seterrorhandler

subroutine `f90calceph_seterrorhandler`(typehandler, userfunc)

  Parameters: | typehandler [INTEGER, intent(in)] :: type of handler userfunc [EXTERNAL, intent(in)] :: user function

This function defines the behavior of the library when an error occurs during the execution of the library's functions. This function should be (not mandatory) called before any other functions of the library. The behavior depends on the value of typehandler.

The possible values for typehandler are :

value | meaning
--- | ---
1 | The library displays a message and continues the execution. The functions return an error code. The python and Octave/Matlab interfaces raise an exception. This is the default behavior of the library.
2 | The library displays a message and terminates the execution with a system call to thefunction exit.
3 | The library calls the user function userfunc with the message.

If the function is called with 1 or 2 for typehandler, the parameter userfunc must be set to 0.

The function userfunc must be defined as

    subroutine userfunc (msg)
    implicit none
    CHARACTER(len=*), intent(in) :: msg

This function must be declared as EXTERNAL