Java : BufferOverflowException with Examples

BufferOverflowException (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the BufferOverflowException methods.


Summary

Unchecked exception thrown when a relative put operation reaches the target buffer's limit.

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 ()

Constructs an instance of this class.

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

Please see the link below.


Related posts

To top of page