Re: on the matter of exceptions
On Mar 8, 8:25 pm, Jonathan Mcdougall <jonathanmcdoug...@gmail.com>
wrote:
I've written a (rather long) article on error handling on general,
particularly
on assertions, exceptions and return values.
I disgree with the way your example checks for logic errors. You use
the C assert macro. I would throw a std::logic_error for a logic
error.
I wonder if it's on-topic but I want to share some low-level
implementation issues - I see that the main issue is how to declare
underlying logic of possible exception-generating code and apart of
that how to alloc and propagate context via execution stack.
a) Registration: at first we register own exception types with
priorities
b) Allocation: instead of on-the-fly allocation of context you have to
allocate exception area (fixed-sized frames) before main flow
c) Propagation: we need own versions of assert, try and catch
communicating with stack
d) Exception handling: for each assertion and try we need to
_explicitly distinguish_ in code between condition and error handler,
because then you can easily predefine underlying logic for crucial
fragments of code
Pseudocode:
#define propagate()
{
free_exception_frame(...); // top of stack
frame = get_exception_frame();
if( ! frame) return;
call(frame.handler());
}
// assertion handler is differentiated from function body for _each_
function separately and must contain propagate()
inline my_fun_assertion_handler_1() { printf("oops, exception in
my_fun()\n"); propagate(); };
// #define assert(cond, recovery, handler)
#define assert(cond, handler)
{
push_exception_frame(type, pri, handler); // , fixer); ?
cond_result = eval(cond);
if(cond_result)
{
// recovery() -> maybe attempt to fix situation here if you know
_logic_ of current problem ?
// I mean that we can propagate logic instead of exception type:
chain of fixers instead of chain of contexts
handler(); // contains propagate
}
pop_exception_frame(...);
return 0;
}
// assert(a > 15, f() { a = 15; global_var_logical_a_error = true; },
my_fun_assertion_handler_1() );
#define try_catch(code, catch, type)
{
code(); // body of try
we_have_ex = check_exception_stack;
if(we_have_ex)
{
pop_exception_frame(type, pri, handler);
if(type.pri = pri)
{
// recovery()
catch(); // body of catch
// finalization here ?
propagate();
}
}
}
#define throw(e)
{
push_exception_frame(e, handler) // handler may be null
propagate();
}
// we can observe that throw() is ~equal to assert(true,
{ push_exception; propagate; } )
etc.
Conclusion:
a) predefined recoveries (func pointers) instead of dynamic contexts
(variable sized) with fixed exception stack frames.
b) if you can write minimal recovery() then you have implicitly coded
logic of exception ...
if you cannot it's "hard" exception, I can say, and we dump core.
What do you think ?
Regards,
Tomasz Budze?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]