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

发布于 2024-10-12 20:41:42 字数 1293 浏览 9 评论 0原文

我正在使用库函数ConnectToTCPServer。当主机不可访问时,此函数会超时。在这种情况下,应用程序崩溃并出现以下错误: “非致命运行时错误:“MyClient.c”,第 93 行,第 15 列,线程 id 0x000017F0:库函数错误(返回值 == -11 [0xfffffff5])。超时错误”

Errorcode 11 是超时错误,因此这种情况可能经常发生在我的应用程序 - 但是应用程序崩溃了 - 我想捕获此错误而不是让我的应用程序崩溃。

我如何捕获 Ansi C90 中的运行时错误?

编辑: 这是当前使用的代码片段:

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;
}

回调函数永远不会被调用。我的服务器未运行,因此 ConnectToTCPServer 将超时。我怀疑回调被调用了 - 但它从未被调用。

编辑2:回调函数实际上没有被调用,ConnectToTCPServer 包含相同的错误信息。我认为这可能是 ConnectToTCPServer 抛出此错误。我只需要抓住它并将其放入 C90 中即可。有什么想法吗?

编辑3:我测试了回调函数,在极少数情况下,我的服务器在线,回调函数实际上被调用 - 但这没有帮助,因为发生错误时不会调用回调。

I am using the library Function 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"

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.

How can i catch this runtime error in Ansi C90?

EDIT:
Here is a Codesnippet of the current use:

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. My Server is not running, so ConnectToTCPServer will timeout. 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. I think it might be a bug that ConnectToTCPServer throws this error. I just need to catch it and bin it in 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

指尖上得阳光 2024-10-19 20:41:42

在查看 NI 文档时,我看到了这一点:
“库错误断点——只要 LabWindows/CVI 库函数在运行时返回错误,您就可以设置一个选项来中断程序执行。”

我推测他们有一个调试选项,可以使程序在运行时错误时停止,您需要在配置、编译时或运行时禁用它。

我的第一个猜测是配置值或编译标志,但这是我找到的唯一选项,它是一个运行时选项:
<代码>
// 如果启用调试,该函数不指示 LabWindows/CVI
// 当National Instruments
时显示运行时错误对话框
// 库函数报告错误。
DisableBreakOnLibraryErrors();

说一下是否有帮助。

Looking in NI documentation, I see this:
"Library error breakpoints -- You can set an option to break program execution whenever a LabWindows/CVI library function returns an error during run time. "

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.

も星光 2024-10-19 20:41:42

标准 C 中不存在“捕获”错误(或“异常”)的一般情况。这由您的库决定如何处理它。可能它会记录其状态,然后简单地调用 abort()。在 Unix 中,这表示可以处理的 SIGABRT 信号,而不仅仅是 exit()ed。或者他们的库可能只是记录日志然后调用 exit()。

您可以在 strace 等实用程序下运行应用程序,以查看正在执行哪些系统调用以及正在断言哪些信号。

如果您无法取得任何进展,我会与您的供应商合作。

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. Likely its logging its state and then simply calling abort(). In Unix, that signals SIGABRT which can be handled and not just exit()ed. Or their library may just be logging and then calling 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.

I'd work with your vendor if you can't make any headway otherwise.

明媚如初 2024-10-19 20:41:42

从文档来看,当发生错误时,您似乎应该调用 clientCallbackFunction 。如果不这样做,您应该编辑您的问题以澄清这一点。

From the documentation, it seems you should get a call to your clientCallbackFunction when an error occurs. If you don't, you should edit your question to clarify that.

时光礼记 2024-10-19 20:41:42

我不确定我是否理解你的意思。

我查看了库函数 ConnectToTCPServer() 的文档。它返回一个int; 0 表示成功,负数是错误代码。

编辑:这是代码片段
目前使用情况:

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

如果这确实是当前使用,您似乎不会尝试判断 ConnectToTCPServer() 是否成功。为此,您需要

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

然后测试 err_code。

ConnectToTCPServer() 的文档意味着除非有来自 TCP 服务器的消息,否则不会调用您的回调函数。没有服务器,没有消息。在这种情况下,

  1. ConnectToTCPServer() 应返回负数。
  2. 您应该检查 ConnectToTCPServer() 的返回值。
  3. 在那里发现负数,你应该做一些明智的事情。

我正确理解了文档吗?

I'm not sure I understand you.

I looked at the documentation for the library function ConnectToTCPServer(). It returns an int; 0 means success, negative numbers are the error codes.

EDIT: Here is a Codesnippet of the
current use:

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. To do that, you'd need

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

and then test err_code.

The documentation for ConnectToTCPServer()implies that your callback function won't be called unless there's a message from a TCP server. No server, no message. In that case,

  1. ConnectToTCPServer() should return a negative number.
  2. You should check the return value of ConnectToTCPServer().
  3. Finding a negative number there, you should do something sensible.

Did I understand the documentation correctly?

缱倦旧时光 2024-10-19 20:41:42

通常,您应该能够简单地检查返回值。您的应用程序退出的事实意味着某些东西已经捕获错误并断言(或类似的东西)。如果没有看到任何上下文(即演示如何使用此函数的代码),就很难变得更精确。

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 (i.e. code demonstrating how you're using this function), it's difficult to be any more precise.

听你说爱我 2024-10-19 20:41:42

文档指出 ConnectToTCPServer 将返回错误代码。仅当连接建立、断开或有数据可供读取时才会调用回调。

您收到的消息指出该错误是非致命的,因此不应中止。如果您确定代码稍后不会中止,那么它看起来确实像是库中的错误。

我不熟悉 CVI,但可能有一个(编译/运行时)选项可以中止,即使是非致命错误(出于调试目的)。如果您可以在一个最小的示例中重现此问题,您应该将其报告给NI。

The documentation states that ConnectToTCPServer will return the error code. 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). If you can reproduce this in a minimal example you should report it to NI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文