Catch C ++ google logging library exception from CHECK macros

I am using a library that uses the google logs library to record errors and validate statements using CHECK macros. In my code, I would like to get an exception if the CHECK conditions do not work (for example, when applications do not open the file because it does not exist), but even when using catch(...) it will not catch anything and the program will crash without control.

Is there a way to catch the exception when CHEC macros fail? Or, if this is not possible, is there a way around these situations?

+5
source share
1 answer

As stated in the glog whitepaper, you can use

Custom Failure Function

to replace the default behavior of exit ().

Example:

  void YourFailureFunction() { throw exception(); } int main(int argc, char* argv[]) { google::InstallFailureFunction(&YourFailureFunction); } 

However, the function is called in the destructor, so the behavior may not correspond to our needs. In my situation, the function is called twice, and I need to implement a not-so-beautiful hack to throw an exception two times.

 bool alreadyThrown = false; void YourFailureFunction() { if (!alreadyThrown) { alreadyThrown = true; throw exception(); } } 
0
source

Source: https://habr.com/ru/post/1215741/


All Articles