System.out的实现中不需要捕获异常吗?
我是java新手,为了弄清楚“System.out”,我阅读了相关的java源代码,然后发现了一些我无法理解的东西。 首先是“System.out”的源代码:
public final static PrintStream out = nullPrintStream();
然后我去了 nullPrintStream
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
我的问题是:程序可能会在函数 nullPrintStream()< 中抛出
NullPointerException
/code>,我们不需要在public final static PrintStream out = nullPrintStream();
中捕获异常吗?为了弄清楚这一点,我在 Eclipse 中编写了一些测试代码,如下所示:
package MainPackage;
public class Src {
private static int throwException() throws Exception{
int m = 1;
if(m == 0) {
throw new Exception();
}
return 0;
}
public static final int aTestObject = throwException(); <==Here i got an error
public static void main(String args[]) {
}
}
就像我想的那样,我得到了一个错误未处理的异常类型异常,但是为什么System .out
可以不处理 NullPointerException
吗?
I am new to java, and to make clear of "System.out", i read relevant java source code, then find something i cannot understand.
First the source code of "System.out":
public final static PrintStream out = nullPrintStream();
then i went to nullPrintStream
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
My question is: the program may throw a NullPointerException
in the function nullPrintStream()
, and we needn't to catch the exception in public final static PrintStream out = nullPrintStream();
? To make clear of it, i wrote some test codes in Eclipse as follows:
package MainPackage;
public class Src {
private static int throwException() throws Exception{
int m = 1;
if(m == 0) {
throw new Exception();
}
return 0;
}
public static final int aTestObject = throwException(); <==Here i got an error
public static void main(String args[]) {
}
}
Just like i think, i got an error Unhandled exception type Exception, but why System.out
is OK without doing with the NullPointerException
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Java 有一个特殊的异常类,称为 RuntimeExceptions 。它们都扩展了 RuntimeException 对象,而 RuntimeException 对象又扩展了 Exception 对象。 RuntimeException(与常规异常相反)的特殊之处在于它不需要显式抛出。几种不同的异常都属于此类,例如
IllegalArgumentException
、IllegalStateException
等...在编码时使用 RTE 的优点是不需要覆盖代码有大量的 try/catch/throws 语句,特别是当异常预计极其罕见且不太可能发生时。此外,如果您有一个捕获 RTE 的通用机制,这也将有助于确保您的应用干净地处理期望条件。
话虽这么说,RTE 可能更难处理,因为从签名来看,特定类或方法是否会抛出该类型的异常并不明显。因此,它们对于 API 来说并不总是一个好主意,除非它们有很好的文档记录。
NullPointerException 是一个 RuntimeException,因此,不需要在方法签名中显式声明。
Java has a special class of Exceptions called RuntimeExceptions. They all extend the
RuntimeException
object, which in turn extends theException
object. The special thing about a RuntimeException (as opposed to a regular exception) is that it does not need to be explicitly thrown. Several different exceptions fit into this category, such asIllegalArgumentException
,IllegalStateException
etc...The advantage of using RTE when you are coding is that you do not need to cover your code with a lot of try/catch/throws statements, especially if the exceptions are expected to be extremely rare and unlikely. Additionally, if you have a general mechanism in place for catching RTE, this will also help make sure your app deals with expection conditions cleanly.
That being said, RTEs can be much more difficult to deal with, as it is not obvious from the signature that a particular class or method will throw that type of exception. Consequently, they are not always a good idea for APIs, unless they are well documented.
A NullPointerException is a RuntimeException, and consequently, does not need to be explicitly declared in the method signature.
NullPointerException
是一个RuntimeException
- 它不需要显式捕获。如果你让你的方法这样做,它就不会在编译时崩溃:
NullPointerException
is aRuntimeException
- it doesn't need to be explicitly caught.if you make your method do this, it won't bomb on compile:
您可能需要初始化静态块中的值并捕获其中的异常。
You can need to intialise the value in a static block and catch the exception there.