{"record":{"id":"0ed662690fab34c6","repo":"apache/beam","slug":"coderexception-exn","errorCode":null,"errorMessage":"CoderException(exn)","messagePattern":"CoderException\\(exn\\)","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/ByteCoder.java","lineNumber":61,"sourceCode":"    if (value == null) {\n      throw new CoderException(\"cannot encode a null Byte\");\n    }\n    outStream.write(value);\n  }\n\n  @Override\n  public Byte decode(InputStream inStream) throws IOException, CoderException {\n    try {\n      // value will be between 0-255, -1 for EOF\n      int value = inStream.read();\n      if (value == -1) {\n        throw new EOFException(\"EOF encountered decoding 1 byte from input stream\");\n      }\n      return (byte) value;\n    } catch (EOFException | UTFDataFormatException exn) {\n      // These exceptions correspond to decoding problems, so change\n      // what kind of exception they're branded as.\n      throw new CoderException(exn);\n    }\n  }\n\n  /**\n   * {@inheritDoc}\n   *\n   * <p>{@link ByteCoder} will never throw a {@link Coder.NonDeterministicException}; bytes can\n   * always be encoded deterministically.\n   */\n  @Override\n  public void verifyDeterministic() {}\n\n  /**\n   * {@inheritDoc}\n   *\n   * @return {@code true}. This coder is injective.\n   */\n  @Override","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/ByteCoder.java#L43-L79","documentation":"ByteCoder.decode catches EOFException and UTFDataFormatException and re-throws them wrapped in CoderException. The visible message is that of the wrapped exception; Beam re-brands these failures so callers can distinguish decode problems from other IO errors.","triggerScenarios":"Decoding a Byte when the stream is at EOF or the underlying read raises UTFDataFormatException (malformed stream contents), i.e. corrupt or truncated single-byte elements.","commonSituations":"Misaligned stream reads, decoding data written by a different coder, truncated serialized data shipped between workers, tests feeding wrong fixture bytes.","solutions":["Catch CoderException at the call site and handle as a data-corruption case","Verify the encoder used to write the stream matches ByteCoder","Re-align stream offsets so decode starts exactly at an encoded Byte","Regenerate corrupted input"],"exampleFix":"// before\nByte b = coder.decode(in); // CoderException propagates and kills the DoFn\n// after\ntry {\n  Byte b = coder.decode(in);\n} catch (CoderException e) {\n  LOG.error(\"Malformed byte element: \" + e.getCause(), e);\n  corruptElements.inc();\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { ... } catch (CoderException e) { Throwable cause = e.getCause(); /* EOFException or UTFDataFormatException: corrupt/truncated input */ }","preventionTips":["Encode and decode with symmetric coders","Check e.getCause() to distinguish EOF vs malformed data"],"tags":["java","beam","decoding","exception-wrapping"],"backgroundTag":"corrupt-decoded-input","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}