Core Exceptions
Overview
The package org.aspose.psd.coreexceptions has two exception classes: PsdLoadException for errors while loading a PSD file and PsdSaveException for errors while saving one. Both extend Exception, so they are checked exceptions, and both have a constructor with a message and a constructor with a message and a cause.
PsdLoadException
PsdImage.load(InputStream) and Image.load(InputStream) declare 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. Messages from the library’s own files: Invalid PSD signature. Expected '8BPS' ..., Unexpected end of PSD/PSB data while reading the file structure. and, for a file that is cut off inside the image resources, a message that the section is longer than the data that remains.
The loader does not check the merged image data at the end of the file, so a file that is cut off or damaged there still loads without an exception.
PsdImage.load(String) and Image.load(String) do not declare the exception. For the same problems they throw a RuntimeException whose cause is the PsdLoadException.
PsdSaveException
PsdImage.save(String) and PsdImage.save(OutputStream) declare only IOException, not PsdSaveException. 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, because the method does not declare it; catch RuntimeException and read getCause().
Other Errors
| Situation | Exception |
|---|---|
| A path that does not exist | FileNotFoundException |
A null path | IllegalArgumentException |
| An unsupported setter | UnsupportedOperationException |
save after close() or dispose() | IllegalStateException |
Tips and Best Practices
- Catch
PsdLoadExceptionseparately fromIOException; it is not a subclass of it. - When you load from a path, look at the cause of a
RuntimeExceptionto find aPsdLoadException. - Show the message of the exception to the user; it names what is wrong with the file.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
catch (IOException e) does not catch a load failure from a stream | PsdLoadException extends Exception | Add catch (PsdLoadException e) |
A RuntimeException when loading by path | The path overload wraps PsdLoadException | Read getCause() |
| A damaged file loads without an exception | The merged image data is not checked | Check your files by other means if completeness matters |
FAQ
Which exception does a file that is not a PSD give?
A PsdLoadException with the message that the PSD signature is invalid, wrapped in a RuntimeException when you load by path.
API Reference Summary
| Class | Description |
|---|---|
| PsdLoadException | Thrown when loading fails |
| PsdSaveException | Exception type that a save problem carries as the cause |