program_options: St9exception Exception?
I posted a question earlier about why I couldn't get Boost 1.33.0 program_options to "see" my positional parameter and why it bombed on unexpected options. Volodya pointed out that an illegal option throws an exception and asked if I had a catch. I felt like a fool for forgetting exceptions and through that was the problem. Then I looked at the code again. The method that uses program_options to read the command line had no try/catch, but the main program that called that method DID have the call wrapped in a try that has catches both for std::exception and (...). And yet, it wasn't trapping anything. Between ZoneAlert and Windows, I *think* I know the problem: something is attempting to access NULL, so I'm getting an OS memory error that C++ exceptions can't catch. I moved try's and catch's around in the code until I found the offending statement: po::variables_map vm; ...a declaration of all things. Here's an excerpt of the code po::options_description desc("Options"); desc.add_options() ("help,h", " Display this help message.") ("version", " Display the version.") // ... other options ;//add-options() // specify the single, required graph file positional parameter po::positional_options_description pd; pd.add("graph_file", 1); po::variables_map vm; //<============== try { po::store(po::command_line_parser(argc, argv). options(desc). positional(pd). run(), vm); po::notify(vm); // ... other code that references vm }//try catch(std::exception e) { cerr << "Exception:" << endl; cerr << e.what() << endl; exit(EXIT_FAILURE); }//catch catch(...) { cerr << "An unknown exception occurs while parsing : "command line arguments." << endl; exit(EXIT_FAILURE); }//catch Using the 'try' in the position shown above, if I input an unexpected command line options, an exceptioin is thrown whose what() method returns "St9exception". That's it. Just "St9exception". I can't find that string in the Boost source anywhere. However, if I move the "try" up one statement so it's above "po::variables_map vm;", then there's no exceptoin, but the access to location 0. That's one picky declaration. Merrill
Merrill Cornish wrote: [...]
}//try catch(std::exception e) { cerr << "Exception:" << endl; cerr << e.what() << endl; exit(EXIT_FAILURE); }//catch catch(...) { cerr << "An unknown exception occurs while parsing : "command line arguments." << endl; exit(EXIT_FAILURE); }//catch
Using the 'try' in the position shown above, if I input an unexpected command line options, an exceptioin is thrown whose what() method returns "St9exception". That's it. Just "St9exception". I can't find that string in the Boost source anywhere.
You are catching the exception by value. That will slice the derived object, and lose the exception info. Catch by reference instead and you might see something more useful. HTH, Ian McCulloch
Merrill Cornish wrote: <snip>
}//try catch(std::exception e) { cerr << "Exception:" << endl; cerr << e.what() << endl; exit(EXIT_FAILURE); }//catch catch(...) { cerr << "An unknown exception occurs while parsing : "command line arguments." << endl; exit(EXIT_FAILURE); }//catch
Using the 'try' in the position shown above, if I input an unexpected command line options, an exceptioin is thrown whose what() method returns "St9exception". That's it. Just "St9exception". I can't find that string in the Boost source anywhere.
In libstdc++ the default messages for standard exception classes are their "mangled" symbol names, and St9exception is the mangled name for std::exception. The reason you're seeing that is that you're catching the exception by value, not reference. Ben.
participants (3)
-
Ben Hutchings
-
Ian McCulloch
-
Merrill Cornish