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

SituationException
A path that does not existFileNotFoundException
A null pathIllegalArgumentException
An unsupported setterUnsupportedOperationException
save after close() or dispose()IllegalStateException

Tips and Best Practices

  • Catch PsdLoadException separately from IOException; it is not a subclass of it.
  • When you load from a path, look at the cause of a RuntimeException to find a PsdLoadException.
  • Show the message of the exception to the user; it names what is wrong with the file.

Common Issues

IssueCauseFix
catch (IOException e) does not catch a load failure from a streamPsdLoadException extends ExceptionAdd catch (PsdLoadException e)
A RuntimeException when loading by pathThe path overload wraps PsdLoadExceptionRead getCause()
A damaged file loads without an exceptionThe merged image data is not checkedCheck 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

ClassDescription
PsdLoadExceptionThrown when loading fails
PsdSaveExceptionException type that a save problem carries as the cause

See Also