0

I'm currently writing a program that reads a lot of pictures from different sources to generate a PDF using PDFBox. Some of these pictures throw an IIOException, which is OK, I catch it and inform the user that the picture can't be read. Some other also throw an IIOException:

javax.imageio.IIOException: JFIF APP0 must be first marker after SOI

that are not caught by my try-catch block. How can I make it so every pictures that throws this Exception have the same behaviour.

Here is my try-catch block :

83    try {
84      if (new File(picture).exists()) {
85          // Some pictures throws an exception here 
86          PDImageXObject pdImage = PDImageXObject.createFromFile(picture, document);
87          contentStream.drawImage(pdImage, x, y, width, height);
88      } else {
89          PDImageXObject pdImage = PDImageXObject.createFromFile(defaultPicture, document);
90          contentStream.drawImage(pdImage, x, y, width, height);
91      }
92          
93  } catch(IIOException e) {
94      PDImageXObject pdImage = PDImageXObject.createFromFile(defaultPicture, document);
95      contentStream.drawImage(pdImage, x, y, width, height);
96      System.out.println("Problem reading this file : " + picture);
97  }

Edit : Stack Trace :

javax.imageio.IIOException: JFIF APP0 must be first marker after SOI
    at com.sun.imageio.plugins.jpeg.JPEGMetadata.<init>(JPEGMetadata.java:224)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.getImageMetadata(JPEGImageReader.java:1023)
    at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.getNumComponentsFromImageMetadata(JPEGFactory.java:213)
    at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.retrieveDimensions(JPEGFactory.java:182)
    at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createFromByteArray(JPEGFactory.java:103)
    at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createFromStream(JPEGFactory.java:85)
    at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromFileByExtension(PDImageXObject.java:241)
    at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromFile(PDImageXObject.java:202)
    at myClass(ServiceGenerationPDF.java:85)
    at 
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)```
Ezlem
  • 11
  • 1
  • Why do you draw the default image in two different blocks - `else` and `catch`? Could the ̀defaultPicture` be wrong so it throws an exception in the `catch` block? – Abrikot Jun 02 '21 at 13:47
  • The picture in the `else` block is drawn in case the supposed picture doesn't exist (not provided, or problem with the name). The one in the `catch` block is in case there is a problem reading the file. I edited my post to include the line number and the stack tace to clarify this :) – Ezlem Jun 02 '21 at 14:36
  • Try using the twelvemonkeys library: https://github.com/haraldk/TwelveMonkeys/ – Tilman Hausherr Jun 05 '21 at 03:35

1 Answers1

0

You shouldn't have actual processing code as a part of catch block. Having said that, it's the PDImageXObject.createFromFile within the catch block that's throwing. Since it's not within it's separate try/catch block, the exception does not get caught.

If you try this code, you will see that every IIOException is captured.

try {
    if (new File(picture).exists()) {
        // Some pictures throws an exception here 
        PDImageXObject pdImage = PDImageXObject.createFromFile(picture, document);
        contentStream.drawImage(pdImage, x, y, width, height);
    } else {
        PDImageXObject pdImage = PDImageXObject.createFromFile(defaultPicture, document);
        contentStream.drawImage(pdImage, x, y, width, height);
    }
        
} catch(IIOException e) {
    System.out.println("Problem reading this file : " + picture);
}
pafau k.
  • 1,667
  • 12
  • 20
  • The line indicated by the error is the one within the if block, so this solution doesn't work. Also the PDImageXObject.createFromFile within the catch block use the same picture as the one in the else block which work. How do you recommend displaying a default picture in the case of the exception ? – Ezlem Jun 02 '21 at 14:00
  • Cache it at program start, and refuse to actually run the program if trying to read default file throws an exception? This is more of a design issue than programming, so that really depends on what you want to do, but if you can't actually read the default picture, this runs contrary to the requirement of displaying it. – pafau k. Jun 02 '21 at 14:04
  • The default picture is OK, and doesn't throw any error. My initial intention was to display this working default picture in case of an error. – Ezlem Jun 02 '21 at 14:07
  • Because you didn't provide code line numbers, stack trace, nor line number where the exception is thrown, the only place where I can *assume* the exception is not caught is the actual catch block. Also if the code I provided runs fine, this means your default picture is not in fact ok. – pafau k. Jun 02 '21 at 14:09
  • I edited to add the line numbers to clarify things :). Your code doesn't work either, so I _figured_ the problem was not with the default picture. – Ezlem Jun 02 '21 at 14:27
  • This... is weird. The only thing I can imagine in this situation is having imported IIOException from some other package than javax.imageio. Could you somehow get this zipped and published somewhere in form of running code and example image that's throwing? Quite interesting behavior, I'd love to see what's wrong myself. Having said that, consider getting a library that can handle formats other than JFIF & EXIF, or is more lenient when parsing [see here](https://stackoverflow.com/questions/55150857/reading-jpeg-or-gif-metadata-with-javax-imageio) – pafau k. Jun 02 '21 at 23:33