Class Http2Exception

All Implemented Interfaces:
Serializable

public final class Http2Exception extends RuntimeException
A connection-level HTTP/2 error. Thrown anywhere a peer's frame, HPACK block, or SETTINGS value violates the protocol in a way that leaves the connection's state (the HPACK dynamic table, a flow-control window, the stream table) unrecoverable. single site: it sends GOAWAY with errorCode() and closes the connection. Compare Http2StreamException, whose scope is one stream and which results in RST_STREAM while the connection survives.

Deliberately does not extend IOException: the connection loop distinguishes a protocol violation (a decision Flash made about the peer's bytes) from a socket failure (the peer went away) by catching these as unrelated types. Conflating them would make it possible to accidentally treat a hostile peer's malformed frame as a harmless disconnect, or vice versa.

Why stack traces are disabled

This exception is thrown on the connection's hot rejection path — a single malformed byte from a hostile or buggy peer can trigger it, and under a scripted attack that can happen many times per second across many connections. JVM stack trace capture (fillInStackTrace) is by far the most expensive part of constructing a Throwable, and it buys nothing here: the call site is exactly where errorCode() says it is, and the debug message already names the specific violation. The 4-argument RuntimeException constructor disables both suppression and stack-trace writing.

Preallocated singletons

For the common, message-less rejections (frame validation failures, HPACK structural errors) this class exposes shared singleton instances. Reusing one instance across threads and across many throws is safe only because the instance carries no per-throw mutable state and writable-stack-trace is disabled — nothing about a throw mutates the exception object.
See Also:
  • Field Details

    • PROTOCOL_ERROR

      public static final Http2Exception PROTOCOL_ERROR
    • FRAME_SIZE_ERROR

      public static final Http2Exception FRAME_SIZE_ERROR
    • FLOW_CONTROL_ERROR

      public static final Http2Exception FLOW_CONTROL_ERROR
    • COMPRESSION_ERROR

      public static final Http2Exception COMPRESSION_ERROR
    • INTERNAL_ERROR

      public static final Http2Exception INTERNAL_ERROR
    • SETTINGS_TIMEOUT

      public static final Http2Exception SETTINGS_TIMEOUT
  • Method Details

    • errorCode

      public Http2ErrorCode errorCode()
      The RFC 9113 §7 error code to send in the GOAWAY frame.
    • of

      public static Http2Exception of(Http2ErrorCode code, String message)
      Builds a connection error carrying a caller-supplied debug message. Allocates a new message carries information specific to this occurrence (e.g. the offending stream id or a decoded value); use one of the preallocated singletons below when it does not.