Core Exceptions

Core Exceptions

Aspose.PSD FOSS for Python defines two exception types specific to PSD/PSB handling: PsdLoadException, raised while parsing a document, and PsdSaveException, raised while writing one. Both derive from the built-in Exception and accept a message plus an optional inner_exception.


Catching Load-Time Failures

PsdLoadException is raised for several structural validation failures while parsing a document:

from aspose_psd_foss.psdimage import PsdImage
from aspose_psd_foss.coreexceptions.psdloadexception import PsdLoadException

try:
    image = PsdImage.load(input_path)
except PsdLoadException as ex:
    print(f"Failed to load PSD/PSB file: {ex}")

Common messages include "Invalid PSD signature. Expected '8BPS' (0x38425053)." (the file doesn’t start with a PSD/PSB signature), "Unsupported PSD version: {n}. ..." (the version field is neither PSD nor PSB), and "{section} length {n} exceeds the enclosing section boundary." (a section’s declared length is inconsistent with the file’s actual size).


Catching Save-Time Failures

PsdSaveException has a single raise site in this FOSS build — a Pascal-string length check:

from aspose_psd_foss.coreexceptions.psdsaveexception import PsdSaveException

try:
    image.save(output_path)
except PsdSaveException as ex:
    print(f"Failed to save PSD/PSB file: {ex}")

Its message is "PSD Pascal string length {n} exceeds the supported maximum 255." — raised when a layer or resource name exceeds 255 bytes.


Inspecting an Inner Exception

Both exception types accept an inner_exception constructor argument, populated when an underlying I/O failure caused the exception:

try:
    image = PsdImage.load(input_path)
except PsdLoadException as ex:
    if ex.inner_exception is not None:
        print(f"Underlying cause: {ex.inner_exception}")

The NotSupportedException Pattern

Several property setters elsewhere in the API raise NotSupportedException rather than PsdLoadException/PsdSaveException — this signals a property that is intentionally read-only in this FOSS build, not a parsing or writing failure. See Resources and Layers for the specific properties affected.


Tips and Best Practices

  • Catch PsdLoadException and PsdSaveException specifically rather than the base Exception when you need to distinguish load failures from save failures.
  • ArgumentNullException (raised when PsdImage.load is called with a None stream) and NotSupportedException are separate exception types — not variants of PsdLoadException/PsdSaveException.

Common Issues

IssueCauseFix
PsdLoadException about an unsupported versionThe file’s version field is neither the standard PSD nor PSB valueConfirm the file is a genuine, uncorrupted PSD/PSB document
PsdSaveException about a Pascal string lengthA layer or resource name exceeds 255 bytesShorten the value before saving

FAQ

Do PsdLoadException and PsdSaveException share a common base exception type?

No — both derive directly from the built-in Exception, not from each other or a shared Aspose.PSD base exception class.

Will catching Exception also catch these two types?

Yes, since both derive from Exception — but catching the specific types lets you distinguish load failures from save failures.


API Reference Summary

Class/MethodDescription
PsdLoadException(message, inner_exception=None)Raised during document parsing
PsdSaveException(message, inner_exception=None)Raised during document writing
NotSupportedException(message, inner_exception=None)Raised by read-only-in-this-FOSS-build setters

See Also