title: Handling exception in ByteToMessageDecoder
body: <p>I wrote a Decoder which extends the ByteToMessageDecoder. Basically, what I wanna do is just to throw an exception (I presume DecoderException) if I've received not expected message, to stop decoding a message and close the connection.</p>
  
  <p>When I throw an exception, it is catched in the following handler which will just log it and close the connection. And I can see it is happening, but then goes the problem...</p>
  
  <p>For some reason I get another message (the same message), which goes straight to Decoder and Decoder tries to decode it again but starting with the next readerIndex, again get unexpected message and throws an exception with the same reason.</p>
  
  <p>Why do I get another same message in the pipeline whereas connection is ment to be closed?</p>
  
  <p>How to prevent this happening?</p>
  
  <pre><code>public class Decoder extends ByteToMessageDecoder
  {
      private static Logger LOG = LoggerFactory.getLogger(Decoder.class);
  
      private Codec codec;
      private int contentSize = 0;
  
      /**
       * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)
       */
      @Override
      protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List&lt;Object&gt; out) throws Exception
      {
          if (!buffer.isReadable())
              return;
  
          // read header
          if (contentSize == 0) {
              int id = buffer.readInt();
              if (id == -1) {
                  LOG.warn("Received null message");
                  throw new DecoderException("Null message has been received.");
              }
              codec = CodecRegistry.getInstance().getCodec(id);
              if (codec == null) {
                  LOG.error("No codec found for id: " + id);
                  throw new DecoderException("No codec found for id: " + id);
              }
              contentSize = buffer.readInt();
          }
  
          if (buffer.readableBytes() &lt; contentSize) {
              return;
          }
  
          // read content
          ByteBufInputStream bbis = new ByteBufInputStream(buffer);
          out.add(codec.decode(bbis));
          contentSize = 0;
      }
  }
  
  @Sharable
  public class ClientHandler extends ChannelHandlerAdapter
  {
      private static Logger LOG = LoggerFactory.getLogger(ClientHandler.class);
  
      /**
       * @see io.netty.channel.ChannelHandlerAdapter#channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
       */
      @Override
      public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
      {
          LOG.info("I got the message!!! -&gt; " + msg);
      }
  
      @Override
      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
      {
          LOG.error("Unexpected exception from downstream." + cause);
          ctx.close();
      }
  }
  
  public class ClientInitializer extends ChannelInitializer&lt;SocketChannel&gt;
  {
      /**
       * @see io.netty.channel.ChannelInitializer#initChannel(io.netty.channel.Channel)
       */
      @Override
      protected void initChannel(SocketChannel ch) throws Exception
      {
          ChannelPipeline pipeline = ch.pipeline();
  
          pipeline.addLast("decoder", new Decoder());
          pipeline.addLast("encoder", new Encoder());
  
          // and then business logic.
          pipeline.addLast("handler", new ClientHandler());
      }
  }
  </code></pre>
  
  <p>The Server sends a message that contains only 8 bytes:</p>
  
  <pre><code>buffer.writeInt(-1);
  buffer.writeInt(12345);
  </code></pre>
  
  <p>So I would expect a <code>DecoderException("Null message has been received.");</code></p>
  
  <p>what I see in the log is:</p>
  
  <pre>
  INFO main Client - Starting Client..........
  WARN nioEventLoopGroup-2-1 Decoder - Received null message
  ERROR nioEventLoopGroup-2-1 ClientHandler - Unexpected exception from downstream.io.netty.handler.codec.DecoderException: org.jasypt.contrib.org.apache.commons.codec_1_3.DecoderException: Null message has been received.
  INFO nioEventLoopGroup-2-1 Client - Disconnected from the server.
  ERROR nioEventLoopGroup-2-1 Decoder - No codec found for id: 12345
  ERROR nioEventLoopGroup-2-1 ClientHandler - Unexpected exception from downstream.io.netty.handler.codec.DecoderException: org.jasypt.contrib.org.apache.commons.codec_1_3.DecoderException: No codec found for id: 12345
  </pre>