Core Exceptions

Core Exceptions

The library reports load and save failures through two dedicated exception types: PsdLoadException and PsdSaveException, both extending the standard Exception class.


PsdLoadException

Thrown when Image.Load() / PsdImage.Load() cannot parse the input as a valid PSD or PSB document.

try
{
    using var image = (PsdImage)Image.Load(inputPath);
}
catch (PsdLoadException ex)
{
    Console.WriteLine($"Failed to load {inputPath}: {ex.Message}");
}

PsdSaveException

Thrown when PsdImage.Save() cannot write the document out.

try
{
    image.Save(outputPath);
}
catch (PsdSaveException ex)
{
    Console.WriteLine($"Failed to save {outputPath}: {ex.Message}");
}

Constructors

Both exception types have the same two-constructor shape: a message-only constructor, and a message-plus-inner-exception constructor for wrapping a lower-level failure.

throw new PsdLoadException("Unexpected end of stream while reading the PSD header.");
throw new PsdSaveException("Failed to write layer data.", innerException);

Tips and Best Practices

  • Catch PsdLoadException around every Image.Load()/PsdImage.Load() call that processes external input.
  • Catch PsdSaveException around every PsdImage.Save() call if the destination (disk, stream) might not be writable.
  • Both types extend Exception, so a broader catch (Exception) also catches them — prefer the specific types when you want to distinguish load failures from save failures.

Common Issues

IssueCauseFix
PsdLoadException on a file that opens fine in other toolsThe input is not a valid PSD/PSB document, or uses a feature this FOSS build’s parser does not recognizeConfirm the file is a genuine PSD/PSB document; report unsupported features separately from load failures
PsdSaveException when writing to a streamThe destination stream may not be writable or may have been disposed alreadyConfirm the stream is open and writable before calling Save()

FAQ

Does this library retry automatically after a load or save failure?

No. PsdLoadException and PsdSaveException are raised once and must be handled by the caller; there is no automatic retry.

Can I inspect the original cause of a wrapped exception?

Yes, when one is provided — both constructors accept an innerException parameter, accessible through the standard Exception.InnerException property.


API Reference Summary

Class/MethodDescription
PsdLoadException(message) / PsdLoadException(message, innerException)Thrown when a document cannot be loaded
PsdSaveException(message) / PsdSaveException(message, innerException)Thrown when a document cannot be saved

See Also