System.err 和 java.lang.Error 有什么区别

What is the difference between System.err and java.lang.Error

我是java的新手。我正在尝试使用 java 实施会议室预订系统。我想把所有的错误信息都打印出来给提示

搜索后发现System.err.println("error 101")java.lang.Error("error 101")都会报错并输出

因此,我的问题是(1)它们之间有什么区别? (2)实施系统时,哪个更有效?

两人互不相干

(1)What is the difference between them?

System.err 是一个 PrintStream 你可以写入,它将输出到控制台的标准错误流,对于控制台中的应用 运行。

Error 是一个错误 class,这是一个 Throwable 表示程序可能无法从中恢复的严重错误。

(2)Which one is more effective when implementing a system?

"More effective" 是一个非常模糊的术语。

在正常情况下,如果您的程序是命令行程序,您可能会向 System.err 输出错误消息。你不太可能直接扔 Error 或 subclass 它;相反,对于 异常 条件,您可能会使用 Exception classes and/or 中的一种。

我建议阅读上面链接的文档,以及 Java Exceptions Tutorial 以了解有关异常处理的更多信息。

这是不同的事情。

System.err.println 向错误流打印一条消息。

java.lang.Error

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

就像一个不应该被捕获的异常。

所以这些想法不能用在同一个上下文中。