News Java exception getMessage () method returns null

Once found View project log when getMessage () return value is null, that is to write code in question, and later found a null pointer return value is null exception, although the cause of the problem is found, but it feels just output null on the log we view the log friendly enough, trying to find a better way.

the reason

After the read API discovery getMessage () method provides class Throwable

getMessage

public String getMessage()

Returns the detail message string of this throwable.

Returns:

the detail message string of this Throwable instance (which may be null).

 It translated meant something: Details Back Trowable thrown in the current instance (may be null)

API can be learned from the description, we getMessage () get is not surprising null, null case bloggers often met is a null pointer exceptions, specifically whether there are other abnormal situation will also not null too well, bloggers read other articles found there are other abnormal situations also return null.

So is there a better way to let us know what is wrong output, the answer is yes, look after some found after two better way:

  • Use of Exception printStackTrace () method
  • Use of Exception toString () method

the difference

When the difference between the null pointer exception occurs Comparison

printStackTrace

When the null pointer exception, will output the number of lines of code exception type and exception where, in the future we multiply the amount of code, there will be a class call another class, an exception will be reported to the output of each row being given, when calling up the complex relationship will output a long list of content.

// call when no other class

java.lang.NullPointerException
    at com.test.HelloWorld.main(HelloWorld.java:12)

// other class or method call when
java.lang.NullPointerException
    AT com.test.HelloWorld.test (HelloWorld.java:11)
    AT com.test.SecondTest.main (SecondTest.java:6)

After a review of the jdk source found NullPointerException itself does not implement toString () function, but the function takes the value detailMessage through inheritance Throwable use of toString () function (null pointer exception detailMessage Throwable class is null, and therefore direct call getMessage () method returns null), if empty return exception class name of the current, otherwise detailMessage, so even null pointer exception will return java.lang.NullPointerException

/**
 * Returns a short description of this throwable.
 * The result is the concatenation of:
 * <ul>
 * <li> the {@linkplain Class#getName() name} of the class of this object
 * <li> ": " (a colon and a space)
 * <li> the result of invoking this object's {@link #getLocalizedMessage}
 *      method
 * </ul>
 * If {@code getLocalizedMessage} returns {@code null}, then just
 * the class name is returned.
 *
 * @return a string representation of this throwable.
 */
public String toString() {
    String s = getClass().getName();
    String message = getLocalizedMessage();
    return (message != null) ? (s + ": " + message) : s;
}

/**
 * Creates a localized description of this throwable.
 * Subclasses may override this method in order to produce a
 * locale-specific message.  For subclasses that do not override this
 * method, the default implementation returns the same result as
 * {@code getMessage()}.
 *
 * @return  The localized description of this throwable.
 * @since   JDK1.1
 */
public String getLocalizedMessage() {
    return getMessage();
}

/**
 * Returns the detail message string of this throwable.
 *
 * @return  the detail message string of this {@code Throwable} instance
 *          (which may be {@code null}).
 */
public String getMessage() {
    return detailMessage;
}

in conclusion

Exception only need to know the type of abnormal return of toString () method, you need to know the details of the error using the Exception of printStackTrace () method.

Caishuxueqian, as described in error, thank you pointed out.

Guess you like

Origin www.cnblogs.com/runningRookie/p/11109728.html