Exceptions

next - skip - up - start

This package includes a partial implementation of exceptions for compilers which do not support C++ exceptions. I used Carlos Vidal's article in the September 1992 C Users Journal as a starting point.

See customising for selecting the correct exception option.

See the section on error messages for some notes on the messages generated by the exceptions.

The rest of this section describes my exception simulation package. But see the end of the section for controlling the action after an exception has been generated.

Newmat does a partial clean up of memory following throwing an exception - see the next section. However, the present version will leave a little heap memory unrecovered under some circumstances. I would not expect this to be a major problem, but it is something that needs to be sorted out.

The functions/macros I define are Try, Throw, Catch, CatchAll and CatchAndThrow. Try, Throw, Catch and CatchAll correspond to try, throw, catch and catch(...) in the C++ standard. A list of Catch clauses must be terminated by either CatchAll or CatchAndThrow but not both. Throw takes an Exception as an argument or takes no argument (for passing on an exception). I do not have a version of Throw for specifying which exceptions a function might throw. Catch takes an exception class name as an argument; CatchAll and CatchAndThrow don't have any arguments. Try, Catch and CatchAll must be followed by blocks enclosed in curly brackets.

I have added another macro Bounce to mean a rethrow, Throw(). This was necessary to enable the package to be compatible with both my exception package and C++ exceptions.

All Exceptions must be derived from a class, Exception, defined in newmat and can contain only static variables. See the examples in newmat if you want to define additional exceptions.

I have defined 5 clases of exceptions for users (there are others but I suggest you stick to these ones):

   SpaceException                 Insufficient space on the heap
   ProgramException               Errors such as out of range index or
                                  incompatible matrix types or
                                  dimensions
   ConvergenceException           Iterative process does not converge
   DataException                  Errors such as attempting to invert a
                                  singular matrix
   InternalException              Probably a programming error in newmat
For each of these exception classes, I have defined a member function void SetAction(int). If you call SetAction(1), and a corresponding exception occurs, you will get an error message. If there is a Catch clause for that exception, execution will be passed to that clause, otherwise the program will exit. If you call SetAction(0) you will get the same response, except that there will be no error message. If you call SetAction(-1), you will get the error message but the program will always exit.