I have a class with a static member:
class MyClass
{
public:
static const SomeOtherClass myVariable;
};
Which I initialize in the CPP file like so:
const SomeOtherClass MyClass::myVariable(SomeFunction());
The problem is, SomeFunction()
reads a value from the registry. If that registry key doesn't exist, it throws an exception. This causes my program to explode without giving the user any useful output... is there some way I can catch the exception so I can log it?
Class ExceptionInInitializerError. Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
Throwing exceptions in static block Herefore, if you throw an exception using the throw keyword in a static block you must wrap it within try-catch blocks else a compile time error will be generated.
A static block can throw only a RunTimeException, or there should be a try and catch block to catch a checked exception. A static block occurs when a class is loaded by a class loader.
We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.
I don't like static
data members much, the problem of initialization being foremost.
Whenever I have to do significant processing, I cheat and use a local static
instead:
class MyClass
{
public:
static const SomeOtherClass& myVariable();
};
const SomeOtherClass& MyClass::myVariable()
{
static const SomeOtherClass MyVariable(someOtherFunction());
return MyVariable;
}
This way, the exception will be throw only on first use, and yet the object will be const
.
This is quite a powerful idiom to delay execution. It had a little overhead (basically the compiler checks a flag each time it enters the method), but better worry about correctness first ;)
If this is called from multiple threads:
boost::once
in the Boost.Threads
libraryconst
, you may not care if it's initialized multiple times, unless someOtherFunction
does not support parallel execution (beware of resources)Guideline: only use static
or global
variables instantiation for simple objects (that cannot throw), otherwise use local static
variables to delay execution until you can catch the resulting exceptions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With