Download ILOG CPLEX 8.1 Getting Started

Transcript
THE ANATOMY OF A CONCERT TECHNOLOGY APPLICATION
Handling Errors
Concert Technology provides two lines of defense for dealing with error conditions, suited
for addressing two kind of errors. The first kind covers simple programming errors.
Examples of this kind are: trying to use empty handle objects or passing arrays of
incompatible lengths to functions.
This kind of error is usually an oversight and should not occur in a correct program. In order
not to pay any runtime cost for correct programs asserting such conditions, the conditions
are checked using assert() statements. The checking is disabled for production runs if
compiled with the -DNDEBUG compiler option.
The second kind of error is more complex and cannot generally be avoided by correct
programming. An example is memory exhaustion. The data may simply require too much
memory, even when the program is correct. This kind of error is always checked at runtime.
In cases where such an error occurs, Concert Technology throws a C++ exception.
To gracefully handle exceptions in a Concert Technology application we advise including all
of the code in a try/catch clause:
IloEnv env;
try {
// ...
} catch (IloException& e) {
cerr << "Concert Exception: " << e << endl;
} catch (...) {
cerr << "Other Exception" << endl;
}
env.end();
Note: The construction of the environment comes before the try/catch clause. In case of
an exception, env.end() must still be called. To protect against failure during the
construction of the environment, another try/catch clause may be added.
If code other than Concert Technology code is used in the part of the above example denoted
by ..., we catch all other exceptions with the statement catch(...). Doing so is good
practice, as it assures that no exception is unhandled.
ILOG CPLEX 8.1 — GETTING STARTED
71
Concert Technology
LibraryTutorial
In fact, Concert Technology provides a hierarchy of exception classes that all derive from
the common base class IloException. Exceptions derived from this class are the only kind
of exceptions that are thrown by Concert Technology. The exceptions thrown by IloCplex
objects all derive from class IloAlgorithm::Exception or IloCplex::Exception.