Errors and messages#

Errors#

All errors and important warnings should use the ERROR definitions in Error_messager.h. The basic messages are summarized below:

Basic error. For GUI programming, you should not expect that the program will terminate after this call. If the platform compiler supports exceptions, a Z7_MAIN_ERROR will be thrown. Please be descriptive with your messages. An example use:

if (bad) ERROR("the calculated result was not ok: "+dtoa(calc));

Use of ERROR will print the file location where the error occurred. Use this call if there is an error reading an ASCII_FILE. It will print the location of the last file read as part of the message. Example:

if (!file.ok) INPUT_ERROR("Trouble reading the precision: "+GLSTR(file));

Use this as a shortcut when a real (double) value is required for input. Use of this function will unify the error messages, so it is strongly recommended. Example:

eps = file.getdouble(); if (!file.ok) DBL_REQ("eps", file);

similar to DBL_REQ for int values. similar to DBL_REQ for vector values. Vectors are in the format ( v1 v2 v3). utility to get a string value for the last token attempted to be read.

Assertions#

It can be useful to perform tests during the execution of the program to test if there are some bugs. This however slows down the program so that it should be done in some particular occasions. The assert function can perform this task. It is implemented as follows (Assert.h):

#ifdef ZCHECK
#define assert(ex) { if (!(ex)){(void)fprintf(stderr,\
     #ex " : assertion failed: file \"%s\", line %d\n",\
      __FILE__, __LINE__);abort();} }
#else
#define assert(ex)
#endif

If ZCHECK is defined (option -DZCHECK while compiling) assert is actually defined. If the test fails, the program stops and indicates the file and the file where the stop was triggered. If ZCHECK is not defined, assert is not compiled and nothing appends.

A good example of this mechanism is given by the generic class ARRAY. When asking for item i using the overloaded operator [] (in Arrayh.).

template <class T> class ARRAY
{
  protected : T* x;
              int size;
....
   T& operator[](int rank)
   { assert(rank>=0 && rank<sz);
     return x[rank];
   }
....

There is another assertion Assert which works similarly, except that it is always active. This function is placed in locations which should technically be impossible for a user to reach. It outputs the stinging message:

Congratulations !! You have generated a Zebulon critical error
XXXX : assertion failed: file SomeFile.c at line XXX

Which sends a user directly to the telephone with no real data for debugging the problem, so don’t use this thing unless the code really will not be reached. Use an ERROR with some diagnostic data instead.