core/vil/vil_exception.h

Go to the documentation of this file.
00001 #ifndef vil_exception_h_
00002 #define vil_exception_h_
00003 //:
00004 // \file
00005 // \brief Exceptions thrown by vil, and a mechanism for turning them off.
00006 // \author Ian Scott.
00007 
00008 #include <vcl_string.h>
00009 #include <vcl_cstdlib.h>
00010 #include <vcl_iostream.h>
00011 #if VCL_HAS_EXCEPTIONS
00012 # include <vcl_stdexcept.h>
00013 #endif
00014 #include <vil/vil_pixel_format.h>
00015 
00016 
00017 //: Throw an exception indicating a definite problem.
00018 // If exceptions have been disabled, this function
00019 // will abort.
00020 template <class T>
00021 void vil_exception_error(T exception)
00022 {
00023 #if !defined VXL_LEGACY_ERROR_REPORTING && VCL_HAS_EXCEPTIONS
00024   throw exception;
00025 #else
00026   vcl_cerr << "\nERROR: " << exception.what() << vcl_endl;
00027   vcl_abort();
00028 #endif
00029 }
00030 
00031 //: Throw an exception indicating a potential problem.
00032 // If exceptions have been disabled, this function
00033 // will return.
00034 template <class T>
00035 void vil_exception_warning(T exception)
00036 {
00037 #if !defined VXL_LEGACY_ERROR_REPORTING && VCL_HAS_EXCEPTIONS
00038   throw exception;
00039 #else
00040   vcl_cerr << "\nWARNING: " << exception.what() << vcl_endl;
00041 #endif
00042 }
00043 
00044   //: Indicates that a function call failed because the pixel types were incompatible.
00045 class vil_exception_pixel_formats_incompatible
00046 #if VCL_HAS_EXCEPTIONS
00047   : public vcl_logic_error
00048 #endif
00049 {
00050  public:
00051   enum vil_pixel_format src_type, dest_type;
00052   vcl_string operation_name;
00053   vil_exception_pixel_formats_incompatible(
00054     enum vil_pixel_format src, enum vil_pixel_format dest, const vcl_string& operation) :
00055 #if VCL_HAS_EXCEPTIONS
00056     vcl_logic_error(operation + ": Pixel formats incompatible."),
00057 #endif
00058     src_type(src), dest_type(dest), operation_name(operation) {}
00059 #if VCL_HAS_EXCEPTIONS
00060   virtual ~vil_exception_pixel_formats_incompatible() throw() {}
00061 #else
00062   const char * what() const {return "Pixel formats incompatible.";}
00063 #endif
00064 };
00065 
00066 
00067 //: Indicates that a function call failed because a pixel format could not be handled.
00068 class vil_exception_unsupported_pixel_format
00069 #if VCL_HAS_EXCEPTIONS
00070   : public vcl_logic_error
00071 #endif
00072 {
00073  public:
00074   enum vil_pixel_format src_type;
00075   vcl_string operation_name;
00076   vil_exception_unsupported_pixel_format(
00077     enum vil_pixel_format src, const vcl_string& operation) :
00078 #if VCL_HAS_EXCEPTIONS
00079     vcl_logic_error(operation + ": Unsupported pixel format."),
00080 #endif
00081     src_type(src), operation_name(operation) {}
00082 #if VCL_HAS_EXCEPTIONS
00083   virtual ~vil_exception_unsupported_pixel_format() throw() {}
00084 #else
00085   const char * what() const {return "Unsupported Pixel formats.";}
00086 #endif
00087 };
00088 
00089 
00090 //: Indicates that some reference was made to pixels beyond the bounds of an image.
00091 // In most cases of out-of-bounds access, you will not get this exception. For efficiency
00092 // reasons, vil may not test for this problem, or may if you are lucky trip an assert.
00093 // This function is only used in cases where easy of use, and risk of mistakes are high,
00094 // and inefficiency is very low.
00095 class vil_exception_out_of_bounds
00096 #if VCL_HAS_EXCEPTIONS
00097   : public vcl_logic_error
00098 #endif
00099 {
00100  public:
00101   vcl_string operation_name;
00102   vil_exception_out_of_bounds(
00103     const vcl_string& operation) :
00104 #if VCL_HAS_EXCEPTIONS
00105     vcl_logic_error(operation + ": Pixel access out-of-bounds."),
00106 #endif
00107     operation_name(operation) {}
00108 #if VCL_HAS_EXCEPTIONS
00109   virtual ~vil_exception_out_of_bounds() throw() {}
00110 #else
00111   const char * what() const {return "Pixel access out-of-bounds.";}
00112 #endif
00113 };
00114 
00115 
00116 //: Indicates that some operation is not supported.
00117 // In most cases you will not get this exception. For efficiency
00118 // reasons, vil may not test for this problem, or may if you are lucky trip an assert.
00119 // This function is only used in cases where easy of use, and risk of mistakes are high,
00120 // and inefficiency is very low.
00121 class vil_exception_unsupported_operation
00122 #if VCL_HAS_EXCEPTIONS
00123   : public vcl_logic_error
00124 #endif
00125 {
00126  public:
00127   vcl_string operation_name;
00128   vil_exception_unsupported_operation(
00129     const vcl_string& operation) :
00130 #if VCL_HAS_EXCEPTIONS
00131     vcl_logic_error(operation + ": Unsupported operation."),
00132 #endif
00133     operation_name(operation) {}
00134 #if VCL_HAS_EXCEPTIONS
00135   virtual ~vil_exception_unsupported_operation() throw() {}
00136 #else
00137   const char * what() const {return "Unsupported operation.";}
00138 #endif
00139 };
00140 
00141 
00142 //: Indicates that an image load or save operation failed.
00143 // Generally this should be thrown, only after checks on the image type
00144 // have been passed by the file format object, and while an
00145 // unrecoverable error is detected inside the image_resource constructor,
00146 // or similar.
00147 class vil_exception_image_io
00148 #if VCL_HAS_EXCEPTIONS
00149   : public vcl_runtime_error
00150 #endif
00151 {
00152  public:
00153 #if !VCL_HAS_EXCEPTIONS
00154   vcl_string full_what;
00155 #endif
00156   vcl_string function_name, file_type, filename, details;
00157   vil_exception_image_io(const vcl_string& function,
00158                          const vcl_string& type,
00159                          const vcl_string& file_name,
00160                          const vcl_string& description = "") :
00161 #if VCL_HAS_EXCEPTIONS
00162     vcl_runtime_error
00163 #else
00164     full_what
00165 #endif
00166     ("Unrecoverable failure in " + function + " while loading "
00167      + file_name + " using " + type + " loader.  Error description: " + description),
00168     function_name(function), file_type(type), filename(file_name), details(description) {}
00169 #if VCL_HAS_EXCEPTIONS
00170   virtual ~vil_exception_image_io() throw() {}
00171 #else
00172   const char * what() const { return full_what.c_str(); }
00173 #endif
00174 };
00175 
00176 
00177 //: Indicates that an image file does not contain the anticipated amount of data
00178 // Generally thrown when an image file's header suggests an image size or
00179 // file length that is not matched by the actual data present in the file
00180 class vil_exception_corrupt_image_file
00181 #if VCL_HAS_EXCEPTIONS
00182   : public vil_exception_image_io
00183 #endif
00184 {
00185  public:
00186   vil_exception_corrupt_image_file(const vcl_string& function,
00187                                    const vcl_string& type,
00188                                    const vcl_string& file_name,
00189                                    const vcl_string& description = "") :
00190 #if VCL_HAS_EXCEPTIONS
00191   vil_exception_image_io(function, type, file_name, description)
00192 #endif
00193   {}
00194 #if VCL_HAS_EXCEPTIONS
00195   virtual ~vil_exception_corrupt_image_file() throw() {}
00196 #else
00197   const char * what() const {return "Image file is corrupt.";}
00198 #endif
00199 };
00200 
00201 
00202 //: Indicating an object with an unknown version number
00203 // Indicates that an image file (or subsection thereof) contains data
00204 class vil_exception_invalid_version
00205 #if VCL_HAS_EXCEPTIONS
00206   : public vil_exception_image_io
00207 #endif
00208 {
00209  public:
00210   vil_exception_invalid_version(const vcl_string& function,
00211                                 const vcl_string& type,
00212                                 const vcl_string& file_name,
00213                                 const vcl_string& description = "")
00214 #if VCL_HAS_EXCEPTIONS
00215   : vil_exception_image_io(function, type, file_name, description)
00216 #endif
00217   {}
00218 #if VCL_HAS_EXCEPTIONS
00219   virtual ~vil_exception_invalid_version() throw() {}
00220 #else
00221   const char * what() const {return "Unknown version number detected in file";}
00222 #endif
00223 };
00224 
00225 #endif // vil_exception_h_

Generated on Mon Nov 23 05:08:36 2009 for core/vil by  doxygen 1.5.1