【发布时间】:2010-11-23 00:49:34
【问题描述】:
我在 Obj-C MyStreamHandler.mm 类文件中创建了一个 libxml 错误处理程序,并使用 xmlTextReaderSetErrorHandler 将其分配给流解析器。在错误处理程序中,我使用[NSException raise:format:] 引发了一个 Obj-C 异常。但是,我的 Obj-C 类的 @try/@catch 块中没有捕获到异常。
在libxml中遇到无效xml(如)时触发回调。错误处理程序的调用是肯定的,并且NSException 也已知会被引发。但是,在作为 libxml 客户端的 Obj-C 类中没有捕获异常。
MyStreamHandler.mm
#import#import "MyStreamHandler.h" void MyErrorCallback(void *arg, const char *msg, xmlParserSeverities 严重性, xmlTextReaderLocatorPtr 定位器); @implementation MyStreamHandler -(void)readStream:(xmlTextReaderPtr)streamAPI { xmlTextReaderSetErrorHandler(streamAPI, (xmlTextReaderErrorFunc)MyErrorCallback, NULL); @尝试 { 而(xmlTextReaderRead(streamAPI)== 1) { // 循环遍历 XML,直到命中格式错误的 XML。 } } @catch (id e) { NSLog(e); } } @结尾 void MyErrorCallback(void *arg,const char *msg,xmlParserSeverities 严重性,xmlTextReaderLocatorPtr 定位器) { [NSException raise:@"MyException" format:@"发生了一些不好的事情:%s",msg]; }
这是一个触发此回调和异常的示例 XML 文件:
下面是由 uncaught 异常引起的相关调用堆栈:
*** 由于未捕获的异常“MyException”而终止应用程序,原因:“发生了一些不好的事情:开始和结束标记不匹配:内容行 0 和容器
'
*** 第一次抛出调用堆栈:
(_NSCallStackArray*)0x10301a40;计数=33 {
0 核心基础 0x04324be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x044795c2 objc_exception_throw + 47
2 CoreFoundation 0x042dd628 + [NSException raise:format:arguments:] + 136
3 CoreFoundation 0x042dd59a +[NSException raise:format:] + 58
4 MyApp 0x00ee1b85 _Z25MyErrorCallbackPvPKc19xmlParserSeveritiesS_ + 319
5 libxml2.2.dylib 0x0401766c xmlTextReaderStandalone + 105
6 libxml2.2.dylib 0x04018793 xmlTextReaderSetErrorHandler + 838
7 libxml2.2.dylib 0x03f69c78 __xmlRaiseError + 1222
8 libxml2.2.dylib 0x03f6d564 名称推送 + 1283
9 libxml2.2.dylib 0x03f75e2c xmlParseXMLDecl + 1291
10 libxml2.2.dylib 0x03f80b6d xmlParseChunk + 3984
11 libxml2.2.dylib 0x0401a255 xmlTextReaderGetAttribute + 816
12 libxml2.2.dylib 0x0401bb34 xmlTextReaderRead + 441
13 MyApp 0x00ec2919 +[MyStreamHandler 读取流:]
...
应用程序调用堆栈
...
24 核心基础 0x0429567d __invoking___ + 29
25 CoreFoundation 0x04295551 - [NSInvocation 调用] + 145
26 基础 0x027bd555 -[NSInvocationOperation main] + 51
27 基础 0x0272bbd2 -[__NSOperationInternal start] + 747
28 基础 0x0272b826 ____startOperations_block_invoke_2 + 106
29 libSystem.B.dylib 0x925a0024 _dispatch_call_block_and_release + 16
30 libSystem.B.dylib 0x925922f2 _dispatch_worker_thread2 + 228
31 libSystem.B.dylib 0x92591d81 _pthread_wqthread + 390
32 libSystem.B.dylib 0x92591bc6 start_wqthread + 30
}
在抛出“NSException”实例后调用终止
这完全是假的吗?我是否能够从 Obj-C++ 函数中抛出 Obj-C 异常并通过我的 Obj-C 代码中的普通 @try/@catch 块来处理它?还是我需要用它自己的 Obj-C++ try/catch 块引发一个实际的 Obj-C++ 异常?
任何帮助将不胜感激。
【问题讨论】:
标签: objective-c libxml2 objective-c++