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 class vil_exception_image_io
00144 #if VCL_HAS_EXCEPTIONS
00145   : public vcl_runtime_error
00146 #endif
00147 {
00148  public:
00149 #if !VCL_HAS_EXCEPTIONS
00150   vcl_string full_what;
00151 #endif
00152   vcl_string function_name, file_type, filename, details;
00153   vil_exception_image_io(const vcl_string& function,
00154                          const vcl_string& type,
00155                          const vcl_string& file_name,
00156                          const vcl_string& description = "") :
00157 #if VCL_HAS_EXCEPTIONS
00158     vcl_runtime_error
00159 #else
00160     full_what
00161 #endif
00162     ("Failed to load " + file_name + " in "
00163      + function + " using " + type + " loader. " + description),
00164     function_name(function), file_type(type), filename(file_name), details(description) {}
00165 #if VCL_HAS_EXCEPTIONS
00166   virtual ~vil_exception_image_io() throw() {}
00167 #else
00168   const char * what() const { return full_what.c_str(); }
00169 #endif
00170 };
00171 
00172 
00173 //: Indicates unexpected problems image file's data.
00174 // For example, can be thrown when an image file's header suggests an image size or
00175 // file length that is not matched by the actual data present in the file
00176 // Generally this should be thrown, only after checks on the image type
00177 // have been passed by the file format object, and while an
00178 // unrecoverable error is detected inside the image_resource constructor,
00179 // get_view(), etc.
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 marked as
00204 // a version which iss unknown to the loading code.
00205 class vil_exception_invalid_version
00206 #if VCL_HAS_EXCEPTIONS
00207   : public vil_exception_image_io
00208 #endif
00209 {
00210  public:
00211   vil_exception_invalid_version(const vcl_string& function,
00212                                 const vcl_string& type,
00213                                 const vcl_string& file_name,
00214                                 const vcl_string& description = "")
00215 #if VCL_HAS_EXCEPTIONS
00216   : vil_exception_image_io(function, type, file_name, description)
00217 #endif
00218   {}
00219 #if VCL_HAS_EXCEPTIONS
00220   virtual ~vil_exception_invalid_version() throw() {}
00221 #else
00222   const char * what() const {return "Unknown version number detected in file";}
00223 #endif
00224 };
00225 
00226 #endif // vil_exception_h_