vil_exception.h
Go to the documentation of this file.
1 #ifndef vil_exception_h_
2 #define vil_exception_h_
3 #define VXL_LEGACY_ERROR_REPORTING // REQUIRED FOR PASSING TESTS 2018-11-02
4 //:
5 // \file
6 // \brief Exceptions thrown by vil, and a mechanism for turning them off.
7 // \author Ian Scott.
8 
9 #include <string>
10 #include <cstdlib>
11 #include <iostream>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 #include <stdexcept>
16 #include <vil/vil_pixel_format.h>
17 
18 
19 //: Throw an exception indicating a definite problem.
20 // If exceptions have been disabled, this function
21 // will abort.
22 template <class T>
23 void vil_exception_error(T exception)
24 {
25 #if !defined VXL_LEGACY_ERROR_REPORTING
26  throw exception;
27 #else
28  std::cerr << "\nERROR: " << exception.what() << std::endl;
29  std::abort();
30 #endif
31 }
32 
33 //: Throw an exception indicating a potential problem.
34 // If exceptions have been disabled, this function
35 // will return.
36 template <class T>
37 void vil_exception_warning(T exception)
38 {
39 #if !defined VXL_LEGACY_ERROR_REPORTING
40  throw exception;
41 #else
42  std::cerr << "\nWARNING: " << exception.what() << std::endl;
43 #endif
44 }
45 
46  //: Indicates that a function call failed because the pixel types were incompatible.
48  : public std::logic_error
49 {
50  public:
51  enum vil_pixel_format src_type, dest_type;
52  std::string operation_name;
54  enum vil_pixel_format src, enum vil_pixel_format dest, const std::string& operation) :
55  std::logic_error(operation + ": Pixel formats incompatible."),
56  src_type(src), dest_type(dest), operation_name(operation) {}
57  ~vil_exception_pixel_formats_incompatible() throw() override = default;
58 };
59 
60 
61 //: Indicates that a function call failed because a pixel format could not be handled.
63  : public std::logic_error
64 {
65  public:
66  enum vil_pixel_format src_type;
67  std::string operation_name;
69  enum vil_pixel_format src, const std::string& operation) :
70  std::logic_error(operation + ": Unsupported pixel format."),
71  src_type(src), operation_name(operation) {}
72  ~vil_exception_unsupported_pixel_format() throw() override = default;
73 };
74 
75 
76 //: Indicates that some reference was made to pixels beyond the bounds of an image.
77 // In most cases of out-of-bounds access, you will not get this exception. For efficiency
78 // reasons, vil may not test for this problem, or may if you are lucky trip an assert.
79 // This function is only used in cases where easy of use, and risk of mistakes are high,
80 // and inefficiency is very low.
82  : public std::logic_error
83 {
84  public:
85  std::string operation_name;
87  const std::string& operation) :
88  std::logic_error(operation + ": Pixel access out-of-bounds."),
89  operation_name(operation) {}
90  ~vil_exception_out_of_bounds() throw() override = default;
91 };
92 
93 
94 //: Indicates that some operation is not supported.
95 // In most cases you will not get this exception. For efficiency
96 // reasons, vil may not test for this problem, or may if you are lucky trip an assert.
97 // This function is only used in cases where easy of use, and risk of mistakes are high,
98 // and inefficiency is very low.
100  : public std::logic_error
101 {
102  public:
103  std::string operation_name;
105  const std::string& operation) :
106  std::logic_error(operation + ": Unsupported operation."),
107  operation_name(operation) {}
108  ~vil_exception_unsupported_operation() throw() override = default;
109 };
110 
111 
112 //: Indicates that an image load or save operation failed.
114  : public std::runtime_error
115 {
116  public:
117  std::string function_name, file_type, filename, details;
118  vil_exception_image_io(const std::string& function,
119  const std::string& type,
120  const std::string& file_name,
121  const std::string& description = "") :
122  std::runtime_error
123  ("Failed to load " + file_name + " in "
124  + function + " using " + type + " loader. " + description),
125  function_name(function), file_type(type), filename(file_name), details(description) {}
126  ~vil_exception_image_io() throw() override = default;
127 };
128 
129 
130 //: Indicates unexpected problems image file's data.
131 // For example, can be thrown when an image file's header suggests an image size or
132 // file length that is not matched by the actual data present in the file
133 // Generally this should be thrown, only after checks on the image type
134 // have been passed by the file format object, and while an
135 // unrecoverable error is detected inside the image_resource constructor,
136 // get_view(), etc.
138  : public vil_exception_image_io
139 {
140  public:
141  vil_exception_corrupt_image_file(const std::string& function,
142  const std::string& type,
143  const std::string& file_name,
144  const std::string& description = "")
145  : vil_exception_image_io(function, type, file_name, description)
146  {}
147  ~vil_exception_corrupt_image_file() throw() override = default;
148 };
149 
150 
151 //: Indicating an object with an unknown version number
152 // Indicates that an image file (or subsection thereof) contains data marked as
153 // a version which iss unknown to the loading code.
155  : public vil_exception_image_io
156 {
157  public:
158  vil_exception_invalid_version(const std::string& function,
159  const std::string& type,
160  const std::string& file_name,
161  const std::string& description = "")
162  : vil_exception_image_io(function, type, file_name, description)
163  {}
164  ~vil_exception_invalid_version() throw() override = default;
165 };
166 
167 #endif // vil_exception_h_
vil_pixel_format
Describes the type of the concrete data.
Indicates that a function call failed because a pixel format could not be handled.
Definition: vil_exception.h:62
vil_exception_corrupt_image_file(const std::string &function, const std::string &type, const std::string &file_name, const std::string &description="")
vil_exception_image_io(const std::string &function, const std::string &type, const std::string &file_name, const std::string &description="")
Indicates that some operation is not supported.
Definition: vil_exception.h:99
vil_exception_invalid_version(const std::string &function, const std::string &type, const std::string &file_name, const std::string &description="")
Indicates that an image load or save operation failed.
Indicating an object with an unknown version number.
Indicates that some reference was made to pixels beyond the bounds of an image.
Definition: vil_exception.h:81
~vil_exception_pixel_formats_incompatible() override=default
Indicates unexpected problems image file's data.
enum vil_pixel_format src_type dest_type
Definition: vil_exception.h:51
Indicates that a function call failed because the pixel types were incompatible.
Definition: vil_exception.h:47
vil_exception_out_of_bounds(const std::string &operation)
Definition: vil_exception.h:86
vil_exception_unsupported_pixel_format(enum vil_pixel_format src, const std::string &operation)
Definition: vil_exception.h:68
void vil_exception_warning(T exception)
Throw an exception indicating a potential problem.
Definition: vil_exception.h:37
vil_exception_pixel_formats_incompatible(enum vil_pixel_format src, enum vil_pixel_format dest, const std::string &operation)
Definition: vil_exception.h:53
void vil_exception_error(T exception)
Throw an exception indicating a definite problem.
Definition: vil_exception.h:23
vil_exception_unsupported_operation(const std::string &operation)