System.out的实现中不需要捕获异常吗?

发布于 2024-12-29 20:46:59 字数 1163 浏览 2 评论 0原文

我是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 技术交流群。

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

发布评论

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

评论(3

梦境 2025-01-05 20:46:59

Java 有一个特殊的异常类,称为 RuntimeExceptions 。它们都扩展了 RuntimeException 对象,而 RuntimeException 对象又扩展了 Exception 对象。 RuntimeException(与常规异常相反)的特殊之处在于它不需要显式抛出。几种不同的异常都属于此类,例如 IllegalArgumentExceptionIllegalStateException 等...

在编码时使用 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 the Exception 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 as IllegalArgumentException, 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.

潦草背影 2025-01-05 20:46:59

NullPointerException 是一个 RuntimeException - 它不需要显式捕获。

如果你让你的方法这样做,它就不会在编译时崩溃:

private static int throwException() throws Exception{
    int m = 1;
    if(m == 0) {
        throw new RuntimeException();
    }
    return 0;
}

NullPointerException is a RuntimeException - it doesn't need to be explicitly caught.

if you make your method do this, it won't bomb on compile:

private static int throwException() throws Exception{
    int m = 1;
    if(m == 0) {
        throw new RuntimeException();
    }
    return 0;
}
紧拥背影 2025-01-05 20:46:59

如果我坚持在 private static int throwException() 中抛出 Exception() ,我应该如何修改 public static final int aTestObject = throwException();

您可能需要初始化静态块中的值并捕获其中的异常。

public static final int aTestObject;
static {
  try {
    aTestObject = throwException();  <==Here i got an error
  } catch (Exception e) {
    throw new AssertionError(e);
  }
}

if i adhere to throw Exception() in private static int throwException() , how should i modify public static final int aTestObject = throwException();

You can need to intialise the value in a static block and catch the exception there.

public static final int aTestObject;
static {
  try {
    aTestObject = throwException();  <==Here i got an error
  } catch (Exception e) {
    throw new AssertionError(e);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文