3

I had been trying hard to figure out why the exceptions thrown from StartElement event handler are not being caught by my application which makes use of expat parser( in C). The application just terminates saying that it cannot find catch blocks, though I have all the catch blocks in place. It is just that since exceptions are being thrown from my own event handlers, XML_Parse API of expat is unable to pass it on to my code where I have all the catch blocks. One of the stackoverflow user with name 'Michael Anderson" suggested to rebuild expat lib with necessary gcc flags to make expat lib handle exceptions. Can someone let me know what flags are those? Or Suggest a better way out to handle errors in event handlers like startelement, endelement etc.

I somehow want XML_Parse API to return 0 if I encounter any exception in my registered event handlers. Please help. Thanks in advance.

Here is the code:

try 
{  
   if( ! XML_Parse(.....) )
   {
        throw exception;
   }
}
catch(...)
{
}

In the working scenario, if XML_Parse encounters a malformed xml file, it promptly returns zero, and I get into if block and throw exception, and it is caught fine. But in the problematic case, the exceptions are being thrown from the event handlers but my application dumps core, and core stack says that it cannot find catch and finally calling std::terminate and abort. Now, how do I make XML_Parse to return zero when I want to throw user defined exception from event handlers?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
user960573
  • 51
  • 5
  • Little code would be very helpful !! – DumbCoder Jun 20 '12 at 11:51
  • try { if (! XML_Parse(......) ) { throw exception } } catch (...){} . This is the catch where I want to get the exception thrown from the handlers registered. In the above code, exception is thrown only when XML_Parse returns 0 in some error situation. But in my problematic case, I am throwing exceptions from my handlers, but my application dumps core instead coming to the above catch, since all the handlers are called by XML_Parse API which is in try block. – user960573 Jun 20 '12 at 12:13
  • Don't throw exceptions from any callbacks that you've provided to expat. That's the sort of thing that can result a `terminate` or `abort` even if you've got a `try {} catch` wrapping the original call that triggers the parsing. – Rook Jun 20 '12 at 13:13
  • Thanks Rook for your reply. But thats what we need to do. What is the other way out? I want to either throw exceptions from callbacks, or make XML_Parse return zero when callbacks encounter any issue which amounts to exception. – user960573 Jun 21 '12 at 05:04

1 Answers1

2

As per expat.h, you should invoke XML_StopParser(parser, 0) when you encounter an error in your handler that warrants aborting the parse.

XML_Parse will then return XML_FALSE. At that point you can invoke your application-specific error handling

Todd Freed
  • 877
  • 6
  • 19