Possible Duplicate:
Difference between try-catch syntax for function
Few days ago I was reading a book about C++ (it cloud be even a Bjarne Stroustrup’s book) and I found such approach in chapter about exceptions:
class Foo :
public Bar
{
// ...
};
// ...
Foo::Foo
try :
Bar ()
{
// ...
}
catch (const std::exception& error)
{
// ...
}
I don’t know why but this construction looks weird for me. However it’s very powerful, because it gives me ability to handle exception thrown by base class “inside” toplevel constructor.
I’m using C++ for few years and I thought, that I know this language pretty good… What’s wrong with this approach? Why it’s not mentioned often event in C++ books?
The real reason is that there is very little you can do in that catch block. You can do something like loging, or throwing a different exception instead, but if you reach the end of the catch block without throwing, the original exception wll be automatically rethrown.
Herb Sutter explains all this very well in this article.