How to Handle PSD Load and Save Errors in Java
This guide shows which error you get for which problem and how to catch it.
Step-by-Step Guide
Step 1: Add the Library
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-psd-foss</artifactId>
<version>26.11.0</version>
</dependency>Step 2: Know the Exceptions
PsdLoadException and PsdSaveException are in org.aspose.psd.coreexceptions. Both extend Exception, so both are checked exceptions.
Step 3: Catch Load Errors From a Stream
PsdImage.load(InputStream) declares PsdLoadException. It is thrown for an invalid signature or header, an empty stream, and data that ends before the end of the image resources or layer sections. Read the message: it names what is wrong, for example Invalid PSD signature. Expected '8BPS'.
Step 4: Catch Load Errors From a Path
PsdImage.load(String) does not declare the exception. For the same problems it throws a RuntimeException whose cause is the PsdLoadException, so read getCause(). A path that does not exist is a FileNotFoundException and a null path is an IllegalArgumentException.
Step 5: Catch Save Errors
PsdImage.save declares only IOException. A save problem, for example a layer name that is longer than 255 characters, surfaces as a RuntimeException whose cause is a PsdSaveException. A catch (PsdSaveException e) around save does not compile; catch RuntimeException and read getCause().
Things to Know
catch (IOException)does not catchPsdLoadException.- A file that is cut off or damaged inside the merged image data at the end of the file still loads without an exception.
- Setters that this edition does not support throw
UnsupportedOperationException, not a PSD exception.