Java : BufferOverflowException 示例

Java 中的 BufferOverflowException (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 BufferOverflowException 方法的代码示例。

注解 :

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

简介

当相对放置操作达到目标缓冲区的限制时,抛出未经检查的异常。 (机器翻译)

Class diagram

final var buffer = CharBuffer.allocate(3);
System.out.println(buffer.capacity()); // 3
System.out.println(buffer.position()); // 0

buffer.put('a');
buffer.put('b');
buffer.put('c');

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [a, b, c]
}

try {
    buffer.put('d');
} catch (BufferOverflowException e) {
    System.out.println("BufferOverflowException!");
}

// Result
// ↓
//BufferOverflowException!

Constructors

BufferOverflowException ()

构造此类的一个实例。 (机器翻译)

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

Methods declared in Throwable

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

请参阅下面的链接。


相关文章

To top of page