简体   繁体   English

Expat解析器-C ++-异常处理

[英]Expat parser - c++ -Exception handling

I have registered three handlers with expat parser: - start -end - text 我已经在expat解析器中注册了三个处理程序:-开始-结束-文本

And from the main program, I read the xml file, buffer it and invoke XML_Parse API. 然后从主程序中读取xml文件,对其进行缓冲并调用XML_Parse API。 Something like this: 像这样:

try {
if( ! XML_Parse (....))
{
   // throw user-defined expection here
}
catch(...)
{
}
} // end of try
catch(...)
{
 }

If XML_Parse returns 0 on failure, an exception is being thrown from inside if. 如果XML_Parse在失败时返回0,则从if内部抛出异常。 And it is caught in inner catch block. 它被捕获在内部捕获块中。

Here is my question: If the user-defined exception is thrown from any of the handlers during parsing, will that be caught in the outer catch ? 这是我的问题:如果在解析过程中从任何处理程序中抛出了用户定义的异常,该异常会被捕获在外部catch中吗?

If yes, its actually not happening in my code. 如果是的话,它实际上并没有在我的代码中发生。 Instead, it is dumping core and stack shows that throw is leading to std:terminate. 取而代之的是,它正在转储核心,并且堆栈表明抛出导致了std:terminate。 Do I have to perform anything else before throwing exceptions from HANDLERS. 在抛出HANDLERS异常之前,我是否还必须执行其他任何操作。

Thanks. 谢谢。

You have a mismatch between try and catch : Each try block is followed by at least one catch block, but you have only one try . trycatch之间不匹配:每个try块后面都至少有一个catch块,但是您只有一次try Maybe like this: 可能是这样的:

try
{
  // stuff before

  try
  {
    if (!parse())
    {
      // ...
    }
  }

  // further catch blocks?

  catch(...)
  {
    // may rethrow
  }

  // stuff after
}

Note that the anonymous catch(...) isn't usually very good design - you either know what you expect and can handle, or you don't need to catch it. 请注意,匿名catch(...)通常不是很好的设计-您要么知道自己的期望并可以处理,要么不需要捕获它。 About the only useful thing for an anonymous catch to do is to log the exception and rethrow it. 匿名捕获要做的唯一有用的事情是记录异常并将其重新抛出。

If you throw an exception from within a try{/*stuff*/} block and the throw was deeply nested, the stack will unwind all the way to the matching outer catch(...) function. 如果从try{/*stuff*/}块中引发异常,并且该throw被深度嵌套,则堆栈将一直展开到匹配的外部catch(...)函数。 If your handlers had assigned heap memory, you will need to deal with that either by using shared_ptr<> or deleting explicitly and carefully . 如果您的处理程序分配了堆内存,则需要使用shared_ptr<>或显式且仔细地删除来处理该内存。 If your handlers were inside the try block, then the exception should behave as normal. 如果您的处理程序位于try块内,则异常应表现为正常。

You have to be very careful with this. 您必须对此非常小心。 (It caused some very hard to track down problems in some code I was working on.). (这在我正在处理的某些代码中造成了一些很难追踪的问题。)。 In my case the expat libs I had to use were not built using the required exception flags in gcc, and since expat is C (rather than C++) it didn't know what to do with the exceptions - when one occurred the application just terminated. 在我的情况下,我必须使用的expat库不是使用gcc中所需的异常标志构建的,并且由于expat是C(而不是C ++),所以它不知道如何处理异常-当发生异常时,应用程序刚刚终止。

However if you can build expat with the right gcc flags all should be OK. 但是,如果您可以使用正确的gcc标志构建expat,则应该一切正常。 (Rebuilding expat wasn't possible for me so I switched to DOM parsing using libxml2 instead). (对我而言,无法重建外籍人士,所以我改用libxml2进行DOM解析)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM