CORBA::Environment provides a way for exception handling when native c++ exception handling is unavailable or undesirable. However, writing portable code for both (with and without) native exception handling capability is very hairy.
ACE provides a set of macros to help dealing with the chaos, but please keep in mind that no amount of macros is going to solve the problem perfectly.
This section explains some simple rules of writing programs for platforms with and without native exception support using ACE's try macros.
ACE try macros are modeled and used like native c++ exceptions with some extra rules to ensure it works even on platforms without native exception support. See some quick examples about how to use ACE try macros.
Name of CORBA::Environment variable
A function that may throw a CORBA::Exception needs a
CORBA::Environment variable to pass up exceptions and to gather
exceptions from functions it called. By default, ACE try macros
assume the variable is named ACE_TRY_ENV
.
ACE_TRY_ENV
itself is also a macro which can be
redefined. If you are using TAO, more likely than not, you
don't have to worry about redefining this macro because TAO
is written to use ACE try macros. For example, you should
define your functions as,
int AN_OBJ::foobar (int a, int b, CORBA_Environment &ACE_TRY_ENV);
and within the function, call other functions that might throw exceptions like,
another_obj->func_name (x, y, ACE_TRY_ENV);
As mentioned, you can redefine the name of the variable to
something else to avoid name clashing. Alternatively, there's
another macro (ACE_ADOPT_CORBA_ENV
) that allow you
to use another variable name as the default CORBA::Environment
within a function.
Throwing exceptions:
Use ACE_THROW
and ACE_THROW_RETURN
to
throw exceptions. They should never be used withing a try
block; please use ACE_TRY_THROW
instead.
Propagating exceptions:
To simulate native exceptions on platforms without native
exception handling, every single function call that may
throw exceptions must be followed by ACE_CHECK
or
ACE_CHECK_RETURN
.
Exception-throwing functions include the following categories:
Any function that takes a
CORBA_Environment
variable.
ACE_NEW_THROW_EX
. Notice that you
should not use ACE_NEW_THROW
,
ACE_NEW_THROW_RETURN
,
ACE_NEW_TRY_THROW
anymore because they don't
work right with ACE try macros. Instead, use
ACE_NEW_THROW
with appropriate ACE_CHECK*
macros.
ACE_TRY
blocks. Follow every
ACE_ENDTRY
with appropriate ACE_CHECK*
macros.
ACE_ENDTRY
with
ACE_CHECK
or ACE_CHECK_RETURN
because
there might be uncaught exception.
You should pass ACE_TRY_ENV
to these
functions.
Be very careful not to combine exception throwing functions in one statement like this:
x = obj1->callme (ACE_TRY_ENV) + obj2->dare_me (ACE_TRY_ENV); ACE_CHECK;
This example may work differently when native exception handling is enabled/disabled.
Catching exceptions:
Use ACE_TRY
to catch exceptions if there's an
ACE_TRY_ENV
available. Otherwise, you should use
ACE_DECLARE_NEW_CORBA_ENV
to create one at
proper scope. The use of
ACE_TRY_NEW_ENV
is considered depricated because it
can't deal with the case when you have multiple TRY
blocks in the scope of ACE_TRY_NEW_ENV
. If there are
more than one try blocks in a function, use ACE_TRY_EX
for all subsequence try blocks to avoid name clashing of labels.
Within a ACE_TRY
block, use the variable
ACE_TRY_ENV
to pass down the
CORBA_Environment
(see this example.)
Follow every exception throwing function with
ACE_TRY_CHECK
, including inner
ACE_ENDTRY
.
Use ACE_CATCH
to catch exceptions of certain
type.
ACE_CATCHANY
catches any exceptions
of type CORBA_Exception
. The caught
exception is stored in a variable call
ACE_ANY_EXCEPTION
.
ACE_CATCHALL
emulate the catch
(...)
c++ statement. It is identical to
ACE_CATCHANY
on platforms without native
exception support. You can not access the caught
exception within the ACE_CATCHALL
block.
Use ACE_RETHROW
to rethrow the same exception
within a ACE_CATCH
or
ACE_CATCHANY
block.
A ACE_TRY
block must be terminated with
a ACE_ENDTRY
statement.
Throw an exception within a ACE_TRY
block or ACE_CATCH
block using
ACE_TRY_THROW
.
Printing out exceptions. Use ACE_PRINT_EXCEPTION
(EX,INFO)
to print out an exception. The macro takes two
arguments, a reference to an exception (EX) and a char
*
string (INFO) which provides more information on the
exception. Since there's no portable way to print out
exceptions, you can redefine ACE_PRINT_EXCEPTION to fit your
need (or define it to null.) You should always print out
the exception itself, not the CORBA_Environment that carries the
exception.
$ACE_ROOT/ace/CORBA_macros.h
for complete definitions of
macros discussed here.
ACE_TRY // Use ACE_DECLARE_NEW_CORBA_ENV to create ACE_TRY_ENV // if you got undefined symbol warning here. { some_operation (arg1, arg2, ACE_TRY_ENV); ACE_TRY_CHECK; . . if (whatever) ACE_TRY_THROW (CORBA::BadParam); some_other_operation (arg1, arg2, arg3, ACE_TRY_ENV); ACE_TRY_CHECK; } ACE_CATCH (CORBA_some_exception, ex) { // error handling. if (still_has_error) ACE_TRY_THROW (CORBA::NOWAY); } ACE_CATCHANY { // error handling. // then rethow the exception. ACE_RETHROW; } ACE_ENDTRY; ACE_CHECK;
ACE_TRY
and also declares a label for internal
use. To avoid defining the same label multiple times within a
function, use ACE_TRY_EX
with different labels for
different try blocks instead. For example,
ACE_TRY_EX (block_1) { some_operation (arg1, arg2, ACE_TRY_ENV); ACE_TRY_CHECK_EX (block_1); some_other_operation (arg1, arg2, arg3, ACE_TRY_ENV); ACE_TRY_CHECK_EX (block_1); } ACE_CATCH (CORBA_some_exception, ex) { // error handling. } ACE_CATCHANY { // error handling. } ACE_ENDTRY; ACE_CHECK_RETURN (-1); // Some other operation here // . // . // . // . ACE_TRY_EX (block_2) { foo (arg, ACE_TRY_ENV); ACE_TRY_CHECK_EX (block_2); bar (arg1, arg2, ACE_TRY_ENV); ACE_TRY_CHECK_EX (block_2); } ACE_CATCH (CORBA_some_exception, ex) { // error handling. } ACE_CATCHANY { // error handling. } ACE_ENDTRY; ACE_CHECK_RETURN (-1);
Be VERY wary of ACE_CATCHALL
. It catches
exceptions of any type. If your program depends on it, then,
more often than not, there're something wrong with it.
Instead of depending on ACE_CATCHALL
, use
auto_prt
style mechanism to prevent memory leaks
from exceptions.
Don't catch an exception just to rethrow it. Exceptions cost you performance.
When exceptions occur, make sure an object's is still in a valid state or change to a state that can be safely destructed.
Watch out for side effect in the expression which may cause
exceptions. In the following example, what should
i
be if an exception does occur?
ACE_TRY { obj[i++] = foo_bar_method (a, b, ACE_TRY_ENV); }
Make sure an exception doesn't cause resource leak (memory, socket, ...) (hint: Use auto_ptr to avoid memory leak, and ACE_Guard for locks.)
Don't catch any exception that you don't know how to handle.
Never throw an exception from destructor (unless you know what it implies.)
Use exceptions to provide more information about the error.
Rethrow a different exception only to provide more
information. Do not catch an exception just to rethrow, say,
unknow_exception
.
As we already mentioned no set of macros can cover all cases
and preserve the semantics between native C++ exceptions and the
CORBA::Environment
based mapping.
Some of the problems that our macros are described below:
Using the macros in loops can produce problems with
break
and continue
statements, for
example:
for (int i = 0; i < 10; ++i) { ACE_TRY { if (x[i] == 0) continue; // will *not* work if (x[i] == -1) break; // will *not* work either } ACE_CATCH (CORBA::Exception, ex) { } ACE_ENDTRY; ACE_CHECK; }
Rename all CORBA_Environment
variables to
ACE_TRY_ENV
.
Replace TAO_TRY
TAO_TRY_VAR
with
ACE_TRY
. Added
ACE_DECLARE_NEW_CORBA_ENV
if necessary.
Replace TAO_TRY_EX
TAO_TRY_VAR_EX
with ACE_TRY_EX
.
Replace TAO_CHECK_RETURN
and
TAO_CHECK_RETURN_VOID
with ACE_CHECK_RETURN
and ACE_CHECK
. These macros are used
outside of TRY/CATCH blocks.
Replace TAO_THROW
, TAO_THROW_ENV
,
TAO_THROW_RETURN
, TAO_THROW_ENV_RETURN
with ACE_THROW
and
ACE_THROW_RETURN
.
Replace TAO_CHECK_ENV
and
TAO_CHECK_ENV_EX
with ACE_TRY_CHECK
and ACE_TRY_CHECK_EX
.
Replace TAO_TRY_THOW
and
TAO_TRY_THROW_EX
with ACE_TRY_THROW
and ACE_TRY_THROW_EX
. Notice that you can also use
ACE_TRY_THROW*
within CATCH blocks.
Replace TAO_RETHROW
,
TAO_RETHROW_RETURN
,
TAO_RETHROW_RETURN_VOID
with
ACE_RETHROW
.
Replace TAO_CATCH
, TAO_CATCHANY
,
and TAO_CATCHALL
with ACE_CATCH
,
ACE_CATCHANY
and ACE_CATCHALL
respectively.
Replace TAO_ENDTRY
with ACE_ENDTRY
followed by an appropriate CHECK macro (ACE_CHECK
,
ACE_CHECK_RETURN
, ACE_TRY_CHECK
, or
ACE_TRY_CHECK_EX
.)