CKTIS / Java
RuntimeException(Throwable): wrapping an exception cause
The RuntimeException(Throwable) constructor creates an unchecked exception, preserving the original cause and deriving its message from that cause.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
new RuntimeException(Throwable cause)RuntimeException(Throwable) creates an exception wrapping the supplied cause. RuntimeException is unchecked: the compiler does not require it to be caught or declared in throws. Using new alone does not throw the constructed exception; throw does that.
getCause() returns the supplied object, and the message is set to (cause==null ? null : cause.toString()), not merely cause.getMessage(). A null value means the cause is absent or unknown; the message is then also null.
Wrapping preserves the cause chain for diagnosis. Do not replace it with error text alone if you want to retain the original exception. To add your own context, use RuntimeException(String message, Throwable cause); that overload does not automatically include the cause's message.
The expression new RuntimeException(null) is ambiguous because of the overloads accepting String and Throwable. Use new RuntimeException((Throwable) null) to select this constructor. After calling it, you cannot reset the cause through initCause(), even if you supplied null.
Examples
public class Main {
public static void main(String[] args) {
Exception cause = new Exception("Input unavailable");
RuntimeException wrapped = new RuntimeException(cause);
System.out.println(wrapped.getMessage());
System.out.println(wrapped.getCause() == cause);
RuntimeException empty = new RuntimeException((Throwable) null);
System.out.println(empty.getMessage() == null);
System.out.println(empty.getCause() == null);
}
}Result
java.lang.Exception: Input unavailable true true true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.