Java : RuntimeException 示例

RuntimeException (Java SE 22 & JDK 22) 示例。
您将在大多数 RuntimeException 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

RuntimeException 是 Java 虚拟机正常运行期间可能抛出的异常的超类。 (机器翻译)

Class diagram

Code examples on this page uses the SampleRuntimeException class below.

@SuppressWarnings("serial")
public class SampleRuntimeException extends RuntimeException {

    public SampleRuntimeException() {
    }

    public SampleRuntimeException(String message) {
        super(message);
    }

    public SampleRuntimeException(String message, Throwable cause) {
        super(message, cause);
    }

    public SampleRuntimeException(Throwable cause) {
        super(cause);
    }

    public SampleRuntimeException(String message, Throwable cause,
                                  boolean enableSuppression,
                                  boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
public class Main {

    public static void main(String[] args) {
        try {
            func1();
        } catch (SampleRuntimeException ex) {

            final RuntimeException e = ex;
            e.printStackTrace();

            // Result
            // ↓
            //SampleRuntimeException
            //	at Main.func2 ...
            //	at Main.func1 ...
            //	at Main.main ...
            // ...
        }
    }

    private static void func1() {
        func2();
    }

    private static void func2() {
        throw new SampleRuntimeException();
    }
}

Constructors

RuntimeException ()

构造一个新的运行时异常,其详细消息为 null。 (机器翻译)

final RuntimeException e = new SampleRuntimeException();
System.out.println(e); // SampleRuntimeException

RuntimeException (String message)

使用指定的详细消息构造一个新的运行时异常。 (机器翻译)

final RuntimeException e = new SampleRuntimeException("abcde");
System.out.println(e); // SampleRuntimeException: abcde
System.out.println(e.getMessage()); // abcde

RuntimeException (String message, Throwable cause)

使用指定的详细消息和原因构造一个新的运行时异常。 (机器翻译)

final RuntimeException cause = new SampleRuntimeException("XYZ");
final RuntimeException e = new SampleRuntimeException("abcde", cause);

System.out.println(e); // SampleRuntimeException: abcde
System.out.println(e.getMessage()); // abcde

System.out.println(e.getCause()); // SampleRuntimeException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

RuntimeException (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)

构造一个新的运行时异常,并指定详细消息、原因、启用或禁用抑制以及启用或禁用可写堆栈跟踪。 (机器翻译)

final RuntimeException cause = new SampleRuntimeException("XYZ");
final RuntimeException e = new SampleRuntimeException("abcde", cause, true, true);

System.out.println(e); // SampleRuntimeException: abcde
System.out.println(e.getCause()); // SampleRuntimeException: XYZ

e.addSuppressed(new SampleRuntimeException("E1"));
e.addSuppressed(new SampleRuntimeException("E2"));

// [SampleRuntimeException: E1, SampleRuntimeException: E2]
System.out.println(Arrays.toString(e.getSuppressed()));

System.out.println(e.getStackTrace().length > 0); // true
// enableSuppression = false
final RuntimeException cause = new SampleRuntimeException("XYZ");
final RuntimeException e = new SampleRuntimeException("abcde", cause, false, true);

System.out.println(e); // SampleRuntimeException: abcde
System.out.println(e.getCause()); // SampleRuntimeException: XYZ

e.addSuppressed(new SampleRuntimeException("E1"));
e.addSuppressed(new SampleRuntimeException("E2"));
System.out.println(Arrays.toString(e.getSuppressed())); // []

System.out.println(e.getStackTrace().length > 0); // true
// writableStackTrace = false
final RuntimeException cause = new SampleRuntimeException("XYZ");
final RuntimeException e = new SampleRuntimeException("abcde", cause, true, false);

System.out.println(e); // SampleRuntimeException: abcde
System.out.println(e.getCause()); // SampleRuntimeException: XYZ

e.addSuppressed(new SampleRuntimeException("E1"));
e.addSuppressed(new SampleRuntimeException("E2"));

// [SampleRuntimeException: E1, SampleRuntimeException: E2]
System.out.println(Arrays.toString(e.getSuppressed()));

System.out.println(e.getStackTrace().length); // 0

RuntimeException (Throwable cause)

使用指定的原因和详细消息(cause==null ? null : cause.toString())(通常包含原因的类和详细消息)构造一个新的运行时异常。 (机器翻译)

final RuntimeException cause = new SampleRuntimeException("XYZ");
final RuntimeException e = new SampleRuntimeException(cause);

System.out.println(e); // SampleRuntimeException: SampleRuntimeException: XYZ
System.out.println(e.getMessage()); // SampleRuntimeException: XYZ

System.out.println(e.getCause()); // SampleRuntimeException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

Methods declared in Throwable

addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString

请参阅下面的链接。


相关文章

To top of page