Using c_exception to Handle complex Mathematics Errors

The c_exception class is not related to the C++ exception handling mechanism that uses the try, catch, and throw statements.

The c_exception class lets you handle errors that are created by the functions and operations in the complex class. When the Complex Mathematics Library detects an error in a complex operation or function, it invokes complex_error(). This friend function of c_exception has a c_exception object as its argument. When the function is invoked, the c_exception object contains data members that define the function name, arguments, and return value of the function that caused the error, as well as the type of error that has occurred. The data members are:

complex arg1;   // First argument of the error-causing function
complex arg2;   // Second argument of the error-causing function
char* name;     // Name of the error-causing function
complex retval; // Value returned by default definition of complex_error
int type;       // The type of error that has occurred.

If you do not define your own complex_error function, complex_error sets the complex return value and the errno error number.

Defining a Customized complex_error Function

You can either use the default version of complex_error() or define your own version of the function. In the following example, complex_error() is redefined:

//Redefinition of the complex_error function
#include <iostream.h>
#include <complex.h>
#include <float.h>
int complex_error(c_exception &c)
{
   cout << "================" << endl;
   cout << "   Exception " << endl;
   cout << "type = " << c.type << endl;
   cout << "name = " << c.name << endl;
   cout << "arg1 = " << c.arg1 << endl;
   cout << "arg2 = " << c.arg2 << endl;
   cout << "retval = " << c.retval << endl;
   cout << "================" << endl;
   return 0;
}
void main()
{
   complex c1(DBL_MAX,0);
   complex result;
   result = exp(c1);
   cout << "exp" << c1 << "= " << result << endl;
} 

This example produces the following output:

================
   Exception
type = 3
name = exp
arg1 = ( 1.79769e+308, 0)
arg2 = ( 0, 0)
retval = ( infinity, -infinity)
================
exp( 1.79769e+308, 0)= ( infinity, -infinity)

If the redefinition of complex_error() in the above code is commented out, the default definition of complex_error() is used, and the program produces the following output:

   exp( 1.79769e+308, 0) = ( infinity, -infinity)

If the redefinition of complex_error() in the above code is commented out, the default definition of complex_error() is used, and the program produces the following output:

   exp( 7.23701e+75, 0) = ( 7.23701e+75, -7.23701e+75)

Compiling a Program that Uses a Customized complex_error Function

If you define your own version of complex_error, you must apply the option link(extdictionary, no) configuration file directive to your target program.