Home

Barry a ¨¦crit :
> After reading Andrei Alexandrescu and Petru Marginean's
> "Generic: Change the Way You Write Exception-Safe Code ¡ª Forever"
> http://www.ddj.com/cpp/184403758
>
> I borrow Boost.Function, which makes the implementation much simpler.
> [snip: good to me]
>
> void MayThrow() throw(int)
> {
> srand(time(0));

srand(time(NULL)) is fine provided you don't have more than one call per
second. Otherwise, all call will yield the same result.
Nothing to do with exception safety though.

> if (int r = rand() % 2)
> throw r;
> }
>
> int main()
> try
> {
> Obj obj;
> Guard guard(bind(&Obj::Rollback, obj));
>
> // do the stuffs, which may throw
> MayThrow();
>
> guard.Commit();
> }
> catch (int i)
> {
> cout << "Exception: i = " << i << endl;
> }
> catch (...)
> {
> cout << "Unknown Exception" << endl;

Avoid the catch all in main() unless you can handle it. You will not
know which exception you don't know about has been thrown.

If you really want to, you can catch std::exception in order to catch
bad_alloc or other derived exception:
catch (exception& e)
{
cout << "Standard exception" << e.what() << endl;
}

> }

A scheme that can be used to extend your system is based on the Memento
GoF pattern. It would consist in making functions performing changes
return a memento structure (a boost::function0 in your case) and append
it to a stack of changes performed. Upon destruction without commit, you
pop the mementos and apply them.

Michael

previous
next

Re: Combine two dictionary...
Re: Retrieving a variable's name.
Re: Algebraic Modules For Python
Re: Wing IDE for Python v. 3.0 beta1 released
Re: MACRO and template with two ( or more ) parameters
Akogo
Niechciane i Zapomniane
Mimo Wszystko
Rodzic Po Ludzku
Fundacja Hobbit