Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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;
}

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);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top