Providing the opportunity to try all the documents and debugging, I could hope that this helps in any way:
@param message the message object to log. @param t the exception to log, including its stack trace. public void error(Object message, Throwable t)
Thus, both cases include a RuntimeException stack trace thrown by code. Not a big difference.
Case 1 : throw new RuntimeException(new NullPointerException("NPE"));
Quote from RuntimeException Java-Doc and NullPointerException Java-Doc
public RuntimeException(Throwable cause)
Throws a new exception at runtime with the specified reason and a verbose message (cause==null ? null : cause.toString()) (which usually contains a message about the class and details of the reason). This constructor is useful for runtime exceptions, which are slightly larger than the wrappers for other metadata.
public NullPointerException(String s)
Throws a NullPointerException with the specified verbose message.
So maybe answer the first part of your question when a java.lang.RuntimeException is cause.toString() at runtime, raised by new NullPointerException , but as cause==null evaluates to false, cause.toString() is printed i.e. java.lang.NullPointerException , and now, since this exception contains a message that follows as NPE
Note. You mentioned the reason as NullPointerException in your code. (hence cause==null evaluates to false)
Case 2 : throw new RuntimeException("RTE", new NullPointerException("NPE"))
public RuntimeException(String message, Throwable cause)
Throws a new exception at runtime with the specified verbose message and reason. Note that the verbose message related to the reason is not automatically included in this verbose runtime exception message.
In this case, you will receive a java.lang.RuntimeException message with an RTE message, since your reason is the child of the RuntimeException itself, and the parent executes it first, in which case the child is not reached.