広告

Java : RuntimeException(非チェック例外) - API使用例

RuntimeException (Java SE 17 & JDK 17) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。


概要

RuntimeExceptionとそのサブクラスは非チェック例外です。

クラス構成

本記事では、RuntimeException のサブクラスの SampleRuntimeException を定義して、それをメインにコード例を記載していきます。

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

SampleRuntimeExceptionの使用例です。

private static class DescriptionTest {

    public class Main {

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

                // 結果
                // ↓
                //SampleRuntimeException
                //	at Main.func2 ...
                //	at Main.func1 ...
                //	at Main.main ...
                // ...
            }
        }

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

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

コンストラクタ

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())を持つ新しい実行時例外を構築します(通常、causeのクラスと詳細メッセージを含みます)。

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

Throwableで宣言されたメソッド

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

Java API 使用例 : Throwable」をご参照ください。


関連記事

ページの先頭へ