Magick::Exception Classes


Exception represents the base class of objects thrown when ImageMagick reports an error.  Magick++ throws C++ exceptions synchronous with the operation when an error is detected. This allows errors to be trapped within the enclosing code (perhaps the code to process a single image)  while allowing the code to be written simply.

A try/catch block should be placed around any sequence of operations which can be considered a unit of work. For example, if your program processes lists of images and some of these images may be defective, by placing the try/catch block around the entire sequence of code that processes one image (including instantiating the image object), you can minimize the overhead of error checking while ensuring that all objects created to deal with that object are safely destroyed (C++ exceptions unroll the stack until the enclosing try block, destroying any created objects).

The pseudocode for the main loop of your program may look like:

for each image in list
  try {
    create image object
    read image
    process image
    save result
  }
  catch( ErrorFileOpen &error )
  {
    process Magick++ file open error
  }
  catch( Exception &error )
  {
    process any Magick++ error
  }
  catch( exception &error )
  {
    process any other exceptions derived from standard C++ exception
  }
  catch( ... )
  {
    process *any* exception (last-ditch effort)
  }

This catches errors opening a file first, followed by any Magick++ exception if the exception was not caught previously.

The Exception class is derived from the C++ standard exception class. This means that it contains a C++ string containing additional information about the error (e.g to display to the user). Obtain access to this string via the what() method.  For example:

 catch( Exception &error_ )
    {
      cout << "Caught exception: " << error_.what() << endl;
    }

The classes Warning and Error derive from the Exception class. Exceptions derived from Warning are thrown to represent non-fatal errors which may effect the completeness or quality of the result (e.g. one image provided as an argument to montage is defective). In most cases, a Warning exception may be ignored by catching it immediately, processing it (e.g. printing a diagnostic) and continuing on. Exceptions derived from Error are thrown to represent fatal errors that can not produce a valid result (e.g. attempting to read a file which does not exist).

The specific derived exception classes are shown in the following tables:
 

Warning Sub-Classes
Warning
Warning Description
WarningUndefined
Unspecified warning type.
WarningResourceLimit
A program resource is exhausted (e.g. not enough memory).
WarningXServer
An X resource is unavailable.
WarningOption
An option was malformed or out of range.
WarningDelegate
An ImageMagick delegate returned an error.
WarningMissingDelegate
The image type can not be read or written because the appropriate Delegate is missing.
WarningCorruptImage
The image file is corrupt (or otherwise can't be read).
WarningFileOpen
The image file could not be opened (permission problem, wrong file type, or does not exist).
WarningBlob
A binary large object could not be allocated.
WarningCache
Pixels could not be saved to the pixel cache.

 
Error Sub-Classes
Error
Error Description
ErrorUndefined
Unspecified error type.
ErrorResourceLimit
A program resource is exhausted (e.g. not enough memory).
ErrorXServer
An X resource is unavailable.
ErrorOption
An option was malformed or out of range.
ErrorDelegate
An ImageMagick delegate returned an error.
ErrorMissingDelegate
The image type can not be read or written because the appropriate Delegate is missing.
ErrorCorruptImage
The image file is corrupt (or otherwise can't be read).
ErrorFileOpen
The image file could not be opened (permission problem, wrong file type, or does not exist).
ErrorBlob
A binary large object could not be allocated.
ErrorCache
Pixels could not be saved to the pixel cache.