title: How to catch sun.awt.image.PNGImageDecoder$PNGException?
body: <p>How to catch the following exception that is printed to the error console when trying to load a corrupted PNG file:</p>
  
  <pre><code>sun.awt.image.PNGImageDecoder$PNGException: invalid depth
      at sun.awt.image.PNGImageDecoder.produceImage(Unknown Source)
      at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
      at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
      at sun.awt.image.ImageFetcher.run(Unknown Source)
  </code></pre>
  
  <p>Following code makes the above output appear in the error console. But the exception is never caught:</p>
  
  <pre><code>import javax.swing.ImageIcon;
  
  public class TestBadPNG
    {
  
      public static void main(String[] args)
        {
          try
            {
              new ImageIcon(new byte[] { -119, 80, 78, 71, 13, 10, 26, 10, 0, });
            }
          catch (Exception e)
            {
              // This line will not be reached.
              System.err.println("Bad image.");
            }
        }
    }
  </code></pre>
  
  <p>Probably the image should be loaded in a different way?</p>
  
  <p>Thanks!</p>