Re: efficient exception handling
terminator wrote:
: On Oct 15, 1:17 pm, Erik Wikstr?m <Erik-wikst...@telia.com> wrote:
::
:: Consider the following, an exception is thrown in a constructor.
:: In the catch-block you use continue to resume the execution which
:: means that
:: your code will continue to run and try to perform operations on a
:: not
:: fully constructed object. Not a good idea. The reason the
:: exception was thrown was that correct execution was no longer
:: possible, the only
:: correct way of handling this is to go back a little and then deal
:: with
:: the situation (by aborting, trying again, trying something
:: different, etc.).
::
:
: I see : Since throw is usually used as a conditional jump, the next
: instruction is executed with the assumption that oposite of the
: condition is true where a continue can not fix the affected data.
: The solution is easy ,one must avoid a 'continue'd catch block for
: old exception classes thrown by hopeless outdated algorithms that
: do not consider a second chance .
:
: A 'throw' statement can reflect the source of error instead of just
: nagging:
:
: class foo{
: public:
: foo()throw(foo*){
: if(some_problem)
: throw this;
: };
: };
If the constructor throws, there will be no foo object. The this
pointer has nothing to point to!
:
: bool fix(foo *);
:
: try{
: foo obj;
: }catch(foo * what){
: if (fix(what))
: continue;//(*what) is valid now.
: throw;//could not solve it:reporting daddy
: }
If you know how to fix the problem for foo, why not just pass it the
fix function in its constructor, and let if fix itself up?
Bo Persson