Magick::Exception Classes

Exception represents the base class of objects thrown when Magick++reports an error. Magick++ throws C++ exceptions synchronous with the operation where the error occurred. 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 with a simple coding style.

A try/catch block should be placed around any sequence of operations which can be considered an important body 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 pseudo code for the main loop of your program may look like:

using namespace std; for infile in list { try { // Construct an image instance first so that we don't have to worry // about object construction failure due to a minor warning exception // being thrown. Magick::Image image; try { // Try reading image file image.read(infile); } catch( Magick::WarningCoder &warning ) { // Process coder warning while loading file (e.g. TIFF warning) // Maybe the user will be interested in these warnings (or not). // If a warning is produced while loading an image, the image // can normally still be used (but not if the warning was about // something important!) cerr << "Coder Warning: " << warning.what() << endl; } catch( Magick::Warning &warning ) { // Handle any other Magick++ warning. cerr << "Warning: " << warning.what() << endl; } catch( Magick::ErrorFileOpen &error ) { // Process Magick++ file open error cerr << "Error: " << error.what() << endl; continue; // Try next image. } try { image.rotate(90); image.write("outfile"); } catch ( MagickExeption & error) { // Handle problem while rotating or writing outfile. cerr << "Caught Magick++ exception: " << error.what() << endl; } } catch( std::exception & error ) { // Process any other exceptions derived from standard C++ exception cerr << "Caught C++ STD exception: " << error.what() << endl; } catch( ... ) { // Process *any* exception (last-ditch effort). There is not a lot // you can do here other to retry the operation that failed, or exit } }

The desired location and number of try/catch blocks in your program depends how sophisticated its error handling must be. Very simple programs may use just one try/catch block.

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


Error Sub-Classes