This section describes the error handling function used for complex math in C++. #include <complex.h> class c_exception {
public:
}; | |||||||||||||||||||||||||||||||||||||
c_exception(char *n, const complex& a1, const complex& a2 = complex_zero); friend int friend complex exp(complex); }; int i = complex_error(c_exception & x) The error handling function complex_error is called if an error occurs for one of the following four functions: friend complex exp(complex) friend complex sinh(complex) friend complex cosh(complex) friend complex log(complex) Users may define their own routines for handling errors, by defining a function named complex_error in their programs. complex_error must be of the form described above. In the class c_exception, the element type is an integer describing the type of error that has occurred, from the following list of constants (defined in the header file <complex.h>):
The element name points to a string containing the name of the function that producedthe error. The variables arg1 and arg2 are the arguments with which the function wasinvoked. retval is set to the default value that is returned by the function unless theuser’s complex_error sets it to a different value. If the user’s complex_error function returns a non-zero value, no error message isprinted, and errno is not set. If the user does not supply a function called complex_error, the default error handlingroutines described under the heading "RETURN VALUES" with the respective functionsare invoked upon error. Default error handling is also summarized in the table below. Inevery case, errno is set to EDOM or ERANGE and the program continues. The following abbreviations are used in the table below:
| |||||||||||||||||||||||||||||||||||||
EXAMPLE | The following program declares a complex number using the default constructor, which #include <iostream.h> #include <complex.h> #include <stdlib.h> int complex_error(c_exception & p) { cerr << "Error when processing "; cerr << p.name << " ( " << p.arg1 << " )\n"; exit (1); return 0; /* NOT REACHED */ } main() { complex c; c = log (c); return 0; } The result of executing the program is: Error when processing log ( ( 0, 0) ) % CCM0998 CPU time used: 0.0005 seconds % CCM0999 exit 1 | ||||||||||||||||||||||||||||||||||||
SEE ALSO | |||||||||||||||||||||||||||||||||||||