简体   繁体   English

如何在ansi C90中捕获运行时错误

[英]How can i catch a runtime error in ansi C90

I am using the library Function ConnectToTCPServer . 我正在使用库函数ConnectToTCPServer This function times out when the host is not reachable. 当主机无法访问时,此功能会超时。 In that case the application crashes with the following error: "NON-FATAL RUN-TIME ERROR: "MyClient.c", line 93, col 15, thread id 0x000017F0: Library function error (return value == -11 [0xfffffff5]). Timeout error" 在这种情况下,应用程序崩溃并出现以下错误:“非致命运行时错误:”MyClient.c“,第93行,第15行,线程ID 0x000017F0:库函数错误(返回值== -11 [0xfffffff5]) 。超时错误“

The Errorcode 11 is a Timeout error, so this could happen quite often in my application - however the application crashes - i would like to catch this error rather than having my application crash. 错误代码 11是一个超时错误,所以这可能在我的应用程序中经常发生 - 但是应用程序崩溃 - 我想抓住这个错误,而不是让我的应用程序崩溃。

How can i catch this runtime error in Ansi C90? 如何在Ansi C90中捕获此运行时错误?

EDIT: Here is a Codesnippet of the current use: 编辑:这是当前使用的Codesnippet:

ConnectToTCPServer(&srvHandle, srvPort, srvName, HPMClientCb, answer, timeout);

with

int HPMClientCb(UINT handle, int xType, int errCode, void *transData){
    printf("This was never printed\n");
    return errCode;
}

The Callbackfunction is never called. 永远不会调用Callback函数。 My Server is not running, so ConnectToTCPServer will timeout. 我的服务器未运行,因此ConnectToTCPServer将超时。 I would suspect that the callback is called - but it never is called. 我怀疑回调被调用 - 但它永远不会被调用。

EDIT 2: The Callback function is actually not called, the Returnvalue of ConnectToTCPServer contains the same error information. 编辑2:实际上没有调用回调函数, ConnectToTCPServer的Returnvalue包含相同的错误信息。 I think it might be a bug that ConnectToTCPServer throws this error. 我认为这可能是ConnectToTCPServer抛出此错误的错误。 I just need to catch it and bin it in C90. 我只需要抓住它并将它装在C90中。 Any Ideas? 有任何想法吗?

EDIT 3: I tested the Callbackfunction, on the rare occaision that my server is online the callback function is actually called - this does not help though because the callback is not called when an error occurs. 编辑3:我测试了Callback函数,在我的服务器在线的罕见的occaision上实际调用了回调函数 - 这没有帮助,因为在发生错误时没有调用回调。

Looking in NI documentation, I see this: 查看NI文档,我看到:
"Library error breakpoints -- You can set an option to break program execution whenever a LabWindows/CVI library function returns an error during run time. " “库错误断点 - 只要LabWindows / CVI库函数在运行时返回错误,您就可以设置一个选项来中断程序执行。”

I would speculate they have a debug option to cause the program to stop on run-time errors, which you need to disable in configuration, in compile time or in run-time. 我推测它们有一个调试选项,可以使程序停止运行时错误,您需要在配置,编译时或运行时禁用它。

My first guess would have been configuration value or compilation flag, but this is the only option I found, which is a run-time option: 我的第一个猜测是配置值或编译标志,但这是我找到的唯一选项,这是一个运行时选项:
// If debugging is enabled, this function directs LabWindows/CVI not
// to display a run-time error dialog box when a National Instruments
// library function reports an error.
DisableBreakOnLibraryErrors();

Say if it helped. 说它是否有帮助。

Theres no such thing as a general case of "catching" an error (or an 'exception') in standard C. Thats up to your library to decide what to do with it. 没有标准C中“捕捉”错误(或“例外”)的一般情况。这取决于你的图书馆决定如何处理它。 Likely its logging its state and then simply calling abort(). 可能它的状态记录,然后简单地调用abort()。 In Unix, that signals SIGABRT which can be handled and not just exit()ed. 在Unix中,它表示可以处理的SIGABRT ,而不仅仅是exit()ed。 Or their library may just be logging and then calling exit(). 或者他们的库可能只是记录,然后调用exit()。

You could run your application under a utility like strace to see what system calls are being performed and what signals are being asserted. 你可以像下一个程序运行你的应用程序strace ,看看有什么正在执行系统调用和什么信号被断言。

I'd work with your vendor if you can't make any headway otherwise. 如果你不能取得任何进展,我会与你的供应商合作。

From the documentation, it seems you should get a call to your clientCallbackFunction when an error occurs. 从文档中,您似乎应该在发生错误时调用clientCallbackFunction If you don't, you should edit your question to clarify that. 如果不这样做,您应该编辑您的问题以澄清这一点。

I'm not sure I understand you. 我不确定我理解你。

I looked at the documentation for the library function ConnectToTCPServer() . 我查看了库函数ConnectToTCPServer()的文档。 It returns an int; 它返回一个int; 0 means success, negative numbers are the error codes. 0表示成功,负数表示错误代码。

EDIT: Here is a Codesnippet of the current use: 编辑:这是当前使用的Codesnippet:

ConnectToTCPServer(&srvHandle, srvPort, srvName, HPMClientCb, answer, timeout);

If that's really the current use, you don't seem to be trying to tell whether ConnectToTCPServer() succeeds. 如果这真的是当前使用,你似乎并没有试图判断ConnectToTCPServer()是否成功。 To do that, you'd need 要做到这一点,你需要

int err_code;
...
err_code = ConnectToTCPServer(&srvHandle, srvPort, srvName, HPMClientCb, answer, timeout);

and then test err_code. 然后测试err_code。

The documentation for ConnectToTCPServer() implies that your callback function won't be called unless there's a message from a TCP server. ConnectToTCPServer()文档意味着除非来自TCP服务器的消息,否则不会调用您的回调函数。 No server, no message. 没有服务器,没有消息。 In that case, 在这种情况下,

  1. ConnectToTCPServer() should return a negative number. ConnectToTCPServer()应返回一个负数。
  2. You should check the return value of ConnectToTCPServer() . 您应该检查ConnectToTCPServer()的返回值。
  3. Finding a negative number there, you should do something sensible. 在那里找到负数,你应该做一些明智的事情。

Did I understand the documentation correctly? 我是否正确理解了文档?

Normally, you should be able to simply check the return value. 通常,您应该能够简单地检查返回值。 The fact that your application exits implies that something is already catching the error and asserting (or something similar). 您的应用程序退出的事实意味着某些东西已经捕获错误并断言(或类似的东西)。 Without seeing any context (ie code demonstrating how you're using this function), it's difficult to be any more precise. 在没有看到任何上下文(即代码演示如何使用此函数)的情况下,很难更精确。

The documentation states that ConnectToTCPServer will return the error code. 该文档指出ConnectToTCPServer将返回错误代码。 The callback is only called if the connection is established, disconnected or when there is data ready to be read. 仅在建立连接,断开连接或准备好读取数据时才调用回调。

The message you get states that the error is NON-FATAL, hence it shouldn't abort. 您收到的消息指出错误是非致命的,因此它不应该中止。 If you're sure the code doesn't abort later it seems indeed like a bug in the library. 如果您确定代码后来没有中止,那么它似乎确实像库中的错误一样。

I'm not familiar with CVI, but there might be a (compile-/runtime-) option to abort even on non-fatal errors (for debugging purposes). 我不熟悉CVI,但可能有(compile- / runtime-)选项即使在非致命错误上也会中止(出于调试目的)。 If you can reproduce this in a minimal example you should report it to NI . 如果您可以在最小的示例中重现这一点,则应将其报告给NI

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

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