How to Handle PSD Core Exceptions in Python

How to Handle PSD Core Exceptions in Python

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

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-psd-foss/Aspose.PSD-FOSS-for-Python.git
cd Aspose.PSD-FOSS-for-Python
# No packaging manifest is published yet -- run from this folder,
# or add it to PYTHONPATH:
export PYTHONPATH=$PWD:$PYTHONPATH

Step 2: Import Required Classes

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

Step 3: Catch Load-Time Failures

PsdLoadException is raised when a document’s structure fails validation — for example, an unsupported PSD/PSB version, a section length that exceeds the enclosing section’s declared boundary, or truncated stream data:

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

Step 4: Catch Save-Time Failures

PsdSaveException is raised while writing a document — for example, when a string value exceeds the length a PSD Pascal string field can hold (255 bytes):

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

Step 5: Inspect an Inner Exception

Both exception types accept an inner_exception constructor argument (used internally, for example, when an I/O error interrupts reading the file structure). Check inner_exception for the underlying cause:

try:
    image = PsdImage.load("input.psd")
except PsdLoadException as ex:
    if ex.inner_exception is not None:
        print(f"Underlying I/O error: {ex.inner_exception}")

Common Issues and Fixes

PsdLoadException with a message about an unsupported version. The file’s declared PSD version field is neither the standard PSD nor the large-document PSB value. Confirm the file is a genuine, uncorrupted PSD/PSB document.

PsdLoadException with a message about a section length exceeding a boundary. The file’s internal length fields are inconsistent with its actual size — this usually indicates a truncated or corrupted file rather than an unsupported variant.

PsdSaveException about a Pascal string length. A string value (such as a layer or resource name) exceeds 255 bytes, the maximum a PSD Pascal string field can encode. Shorten the value before saving.

Frequently Asked Questions

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.

Can PsdSaveException occur for reasons other than a long string value?

The confirmed raise site in this FOSS build is the Pascal-string-length check. Other save-time I/O failures (for example, a destination stream that cannot be written to) surface as the underlying Python exception type rather than being wrapped in PsdSaveException.

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.

See Also