Error Message with System.err.println() before System.exit(-1) in Java

huangapple go评论61阅读模式
英文:

Error Message with System.err.println() before System.exit(-1) in Java

问题

我有一个关于Java虚拟机崩溃而没有错误消息的问题。我经常在我的代码中进行一些检查。如果其中一个检查未被满足,Java程序将出现问题。

例如:

if (!constraint) {
   System.err.println("约束未满足!");
   System.exit(-1);
}

我可以确定在使用System.exit(-1)之前会在控制台上打印出带有System.err.println("...")的错误消息吗?如果不是这样,这可能解释了为什么我的Java虚拟机在没有错误消息的情况下崩溃。

英文:

I have a problem with crashed Java Virtual Mashines without error message. I often do some checks in my code. Something went wrong, if one of these checks will not be fulfilled and the java program shall not carry on.
E.G.:

if(!constraint){
   System.err.println("The constraint is not fullfilled!");
   System.exit(-1);
}

Can I be sure, that an error message with System.err.println("...") is printed on the console before the JVM is killed with System.exit(-1)? If not, this could explain why my JVM crashes without error message.

答案1

得分: 1

System.out将输出导向标准输出流(通常映射到控制台屏幕)。System.err将输出导向标准错误流(默认情况下也导向控制台)。拥有这两者的想法是,标准输出应用于常规程序输出,而标准错误应用于错误消息。根据加载Java程序的方式,错误流是否打印在控制台上会有所不同。

这两个流可以重定向到不同的目标。如果希望将输出重定向到一个输出文件,将错误消息重定向到不同的日志文件,那么在UNIX上可以这样做:

java MyClass > output.log 2> error.log

这将导致常规输出(使用System.out)存储在output.log中,而错误消息(使用System.err)存储在error.log中。

另一种选项是在代码中重定向输出:

PrintStream outStream = new PrintStream(new File("outFile.txt"));
System.setOut(outStream);
System.out.println("This is a baeldung article");

对于System.err:

PrintStream errStream = new PrintStream(new File("errFile.txt"));
System.setErr(errStream);
System.err.println("This is a baeldung article error");

System.out和System.err不是更好的解决方案来检查这个问题,我建议添加一个像SL4J这样的记录器,以将其记录在适当的文件或输出中。

英文:

System.out leads the output to the standard output stream (normally mapped to the console screen). System.err leads the output to the standard error stream (and, by default to the console). The idea behind having these two is that the standard output should be used for regular program output, and standard error should be used for error messages. Depend how to load a java program the error stream is not printed on console.

Both the streams can be redirected to different destinations. If it is desired to redirect the output to an output file and error messages to a different log file, than on UNIX it can be done as follows:

java MyClass > output.log 2>error.log

This causes the regular output (using System.out) to be stored in output.log and error messages (using System.err) to be stored in error.log.

Another option is in your code redirect the outputs:

PrintStream outStream = new PrintStream(new File("outFile.txt"));
System.setOut(outStream);
System.out.println("This is a baeldung article");

And in case of System.err:

PrintStream errStream = new PrintStream(new File("errFile.txt"));
System.setErr(errStream);
System.err.println("This is a baeldung article error");

The System.out and System.err is not a better solution to check this, I suggest to add a logger like SL4J to log this in appropriate file or output.

huangapple
  • 本文由 发表于 2020年8月9日 01:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63318457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定