私は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();
}
私の質問は次のとおりです。プログラムはNullPointerException
関数nullPrintStream()
にaをスローする可能性があり、例外をキャッチする必要はありません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[]) {
}
}
私が思うように、未処理の例外タイプExceptionSystem.out
というエラーが発生しましたが、?を使用せずにOKなのはなぜNullPointerException
ですか?