Re: handling exception without stderr printings



The printing to stderr is done by libxml2, not by libxml++. It was not as easy as I thought to find out how to avoid it, and the solution I found is somewhat bizarre.
The libxml2 function initGenericErrorDefaultFunc() takes a pointer to a function pointer.

#include <libxml++/libxml++.h>
#include <libxml/xmlerror.h>
#include <iostream>
#include <stdlib.h>

using namespace std;
using namespace xmlpp;

static void myGenericErrorFunc(void* /* parsing context */,
  const char* /* format (like fprintf) */, ...)
{
  // Don't print anything.
  // libxml2's default GenericErrorFunc prints to stderr.
}

static xmlGenericErrorFunc pGenericErrorFunc = myGenericErrorFunc;

int main()
{
initGenericErrorDefaultFunc(&pGenericErrorFunc);

try
{
xmlpp::DomParser parser;
parser.set_throw_messages(true);
parser.parse_file("config.xml");
}
catch (const xmlpp::exception& ex)
{
cout << ex.what() << endl;
}

return 0;
}

2013-07-30 23:12, Pablo Madoery skrev:


The problem i have is the following:
In the case that "config.xml" doesn't exist I want that only exception cout be printed (and not stderr).
But that's not the case. The exception is raised and the stderr is printed too.
How can i make that stderr dont be printed.
Can you help me?.
Thank you very much.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]