简体   繁体   English

为什么不在 stderr 上打印浮点错误消息?

[英]Why floating point error message is not printing on stderr?

I am running c language code using a java application using java.lang.ProcessBuilder .我正在使用 java.lang.ProcessBuilder 使用java.lang.ProcessBuilder应用程序运行 c 语言代码。 If any output and error generated by this c language code, I am able to read those via process.getInputStream() and process.getErrorStream() .如果有任何 output 和此 c 语言代码生成的错误,我可以通过process.getInputStream()process.getErrorStream()读取它们。 But in some cases (when exit code = 136 or 139) like if the code execution failed due to floating point error , I am not getting any error in process's error stream. So, I tried to run my c language code directly on shell and redirected stderr and stdout to seperate files.但在某些情况下(当退出代码 = 136 或 139 时),例如代码执行因floating point error而失败,我在进程错误 stream 中没有收到任何错误。因此,我尝试直接在 shell 上运行我的 c 语言代码,并且将 stderr 和 stdout 重定向到单独的文件。 Code.c编号.c

#include<stdio.h>

int main() {
    int x = 1/0;
    return 0;
}

Command I am running:我正在运行的命令:

# gcc Code.c -o Code.out -std=c99
# ./Code.out 2>err.log 1>out.log
Floating point exception (core dumped)

As you can see above error is printing on shell only but not redirecting to err.log .如您所见,上面的错误仅打印在 shell 上,但没有重定向到err.log Also, nothing is there in err.log.此外,err.log 中没有任何内容。 My question is why this is not printing in stderr stream?我的问题是为什么这不在 stderr stream 中打印? As per my understanding if it is not printing in stderr I will not be able to read it via process.getErrorStream.根据我的理解,如果它没有在 stderr 中打印,我将无法通过 process.getErrorStream 读取它。 Whatever the error message generated by code execution, I wanted to show it to end user.无论代码执行生成什么错误消息,我都想将其显示给最终用户。

As dave_thompson_085 comments, the Floating point exception (core dumped) message is from the Shell rather than the program.正如 dave_thompson_085 评论的那样, Floating point exception (core dumped)消息来自 Shell 而不是程序。

This is a test program:这是一个测试程序:

#include<stdio.h>

int main() {
    fprintf(stdout, "normal output, %d\n", __LINE__);
    fprintf(stderr, "error output, %d\n", __LINE__);
    fflush(stdout);
    fflush(stderr);

    int x = 1/0;

    fprintf(stdout, "normal output, %d\n", __LINE__);
    fprintf(stderr, "error output, %d\n", __LINE__);
    fflush(stdout);
    fflush(stderr);

    return 0;
}
# gcc code.c -o Code -std=c99
# ./Code  2>err.log 1>out.log
normal output, 4
error output, 5
Floating point exception (core dumped)

# cat out.log
normal output, 4

$cat err.log
error output, 5

When the float exception occurs, it is captured by the OS and forced to exit.当float异常发生时,会被OS捕获并强制退出。

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

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