static member initialization in a try/catch block?

Ian Lance Taylor iant@google.com
Sat Jun 5 12:15:00 GMT 2010


Bernd Prager <bernd@prager.ws> writes:

> I am trying to design a cache object (singleton) that gets the desired
> object directly from the database if it is not already in the cache.
>
> The db connection is a static class member:
>
> Cache.h:
> class Cache {
>     static log4cxx::LoggerPtr logger;
>     static pqxx::connection dbCon;
> public:
>    ...
> }
>
> The connection is going to be initialized in the source file Cache.cpp
> before the class methods:
>
> ...
> pqxx::connection
>     Cache::dbCon("user=postgres password=xxxx dbname=mydb
> hostaddr=127.0.0.1 port=5432");
>
> Cache::Cache() { ... }
> ...
>
> The connection constructor could throw an exception. How do I catch that?
> Java allows a static block to handle that, C++ AFAIK doesn't.
> I was googleing it but could not find anything; neither in FAQ's. Its
> probably trivial so please be nice to me. ;-)


This isn't a gcc question.  It's a C++ question.  You may get a better
answer if you ask in a C++ language forum.

As far as I know there is no way in C++ to catch an exception thrown
by the constructor of a global variable.

Ian



More information about the Gcc-help mailing list