### Example (_matherr -- Process Math Library Errors)

This example provides a _matherr function to handle errors from the log or log10 functions. The arguments to these logarithmic functions must be positive double values. _matherr processes a negative value in an argument (a domain error) by returning the log of its absolute value. It suppresses the error message normally displayed when this error occurs. If the error is a zero argument or if some other routine produced the error, the example takes the default actions.

Note: You must compile this example with the link"/NOE" compiler option.

    #include <math.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
       int value;

       printf("Trying to evaluate log10(-1000.00) will create a math exception.\n");
       value = log10(-1000.00);
       printf("The _matherr() exception handler evaluates the expression to \n");
       printf("log10(-1000.00) = %d\n", value);
       return 0;

       /*************************************************************************
          The output should be:

          Trying to evaluate log10(-1000.00) will create a math exception.
          inside _matherr
          The _matherr() exception handler evaluates the expression to
          log10(-1000.00) = 3
       *************************************************************************/
    }
    int _matherr(struct _exception *x)
    {
       printf("inside _matherr\n");
       if (DOMAIN == x->type) {
          if (0 == strcmp(x->name, "log")) {
             x->retval = log(-(x->arg1));
             return EXIT_FAILURE;
          }
          else
             if (0 == strcmp(x->name, "log10")) {
                x->retval = log10(-(x->arg1));
                return EXIT_FAILURE;
             }
          }
          return 0;                                    /* Use default actions */
    }