How to Handle PSD Core Exceptions in .NET

How to Handle PSD Core Exceptions in .NET

Aspose.PSD FOSS for .NET defines two exception types: PsdLoadException, thrown while parsing a PSD/PSB document, and PsdSaveException, thrown while writing one. Both derive from System.Exception and support a message-only constructor and a message-plus-inner-exception constructor.

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-psd-foss/Aspose.PSD-FOSS-for-.NET.git
cd Aspose.PSD-FOSS-for-.NET
dotnet build

Step 2: Import Required Classes

using Aspose.PSD.FileFormats.Psd;

Step 3: Catch Load-Time Failures

PsdLoadException is thrown 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
{
    using var image = PsdImage.Load("input.psd");
}
catch (PsdLoadException ex)
{
    Console.WriteLine($"Failed to load PSD/PSB file: {ex.Message}");
}

Step 4: Catch Save-Time Failures

PsdSaveException is thrown while writing a document — for example, when a string value exceeds the length a PSD Pascal string field can hold:

try
{
    image.Save("output.psd");
}
catch (PsdSaveException ex)
{
    Console.WriteLine($"Failed to save PSD/PSB file: {ex.Message}");
}

Step 5: Inspect an Inner Exception

Both exception types have a constructor that wraps an inner exception (used internally, for example, when an I/O error interrupts reading the file structure). Check InnerException for the underlying cause:

try
{
    using var image = PsdImage.Load("input.psd");
}
catch (PsdLoadException ex) when (ex.InnerException != null)
{
    Console.WriteLine($"Underlying I/O error: {ex.InnerException.Message}");
}

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 System.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 throw 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 .NET exception type rather than being wrapped in PsdSaveException.

Will catching Exception also catch these two types?

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

See Also