Errors and messages#

Errors#

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

ERROR

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.

INPUT_ERROR

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));
DBL_REQ

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);
INT_REQ

similar to DBL_REQ for integer values.

VEC_REQ

similar to DBL_REQ for vector values. Vectors are in the format ( v1 v2 v3).

GLSTR

utility to get a string value for the last token attempted to be read.

Assertions#

During program execution, it can be useful to perform tests to detect bugs. However, this slows down the program, so it should be done only in specific situations. The assert function can be used for this purpose. 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 when compiling), assert is actually defined. If the test fails, the program stops and indicates the file and line where the stop occurred. If ZCHECK is not defined, assert is not compiled and nothing happens.

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 following message:

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

This provides no useful debugging information, so Assert should be used only for code that should never be reached. For diagnostics, use ERROR with relevant data instead.