広告

Java : BufferUnderflowException - API使用例

BufferUnderflowException (Java SE 23 & JDK 23) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。


概要

相対get操作がソース・バッファのリミットに達したときにスローされる非チェック例外です。

クラス構成

BufferUnderflowException は非チェック例外です。
Buffer 操作でリミットを超えた要素を取得しようとすると発生します。

final char[] array = {'a', 'b', 'c'};
final var buffer = CharBuffer.wrap(array);

{
    final var dst = new char[3];
    final var ret = buffer.get(dst);
    System.out.println(ret.position()); // 3

    System.out.println(Arrays.toString(dst)); // [a, b, c]
}

try {
    final var dst = new char[1];
    var _ = buffer.get(dst);
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// 結果
// ↓
//BufferUnderflowException!

コンストラクタ

BufferUnderflowException ()

このクラスのインスタンスを構築します。

final var e = new BufferUnderflowException();
System.out.println(e); // java.nio.BufferUnderflowException

Throwableで宣言されたメソッド

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

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


関連記事

ページの先頭へ