Point Cloud Library (PCL)  1.7.0
exceptions.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 #ifndef PCL_EXCEPTIONS_H_
38 #define PCL_EXCEPTIONS_H_
39 
40 #include <stdexcept>
41 #include <sstream>
42 #include <pcl/pcl_macros.h>
43 #include <boost/current_function.hpp>
44 
45 /** PCL_THROW_EXCEPTION a helper macro to be used for throwing exceptions.
46  * This is an example on how to use:
47  * PCL_THROW_EXCEPTION(IOException,
48  * "encountered an error while opening " << filename << " PCD file");
49  */
50 #define PCL_THROW_EXCEPTION(ExceptionName, message) \
51 { \
52  std::ostringstream s; \
53  s << message; \
54  s.flush (); \
55  throw ExceptionName(s.str(), __FILE__, BOOST_CURRENT_FUNCTION, __LINE__); \
56 }
57 
58 namespace pcl
59 {
60 
61  /** \class PCLException
62  * \brief A base class for all pcl exceptions which inherits from std::runtime_error
63  * \author Eitan Marder-Eppstein, Suat Gedikli, Nizar Sallem
64  */
65  class PCLException : public std::runtime_error
66  {
67  public:
68 
69  PCLException (const std::string& error_description,
70  const std::string& file_name = "",
71  const std::string& function_name = "" ,
72  unsigned line_number = 0) throw ()
73  : std::runtime_error (error_description)
74  , file_name_ (file_name)
75  , function_name_ (function_name)
76  , message_ (error_description)
77  , line_number_ (line_number)
78  {
80  }
81 
82  virtual ~PCLException () throw ()
83  {}
84 
85  const std::string&
86  getFileName () const throw ()
87  {
88  return (file_name_);
89  }
90 
91  const std::string&
92  getFunctionName () const throw ()
93  {
94  return (function_name_);
95  }
96 
97  unsigned
98  getLineNumber () const throw ()
99  {
100  return (line_number_);
101  }
102 
103  std::string
104  detailedMessage () const throw ()
105  {
106  std::stringstream sstream;
107  if (function_name_ != "")
108  sstream << function_name_ << " ";
109 
110  if (file_name_ != "")
111  {
112  sstream << "in " << file_name_ << " ";
113  if (line_number_ != 0)
114  sstream << "@ " << line_number_ << " ";
115  }
116  sstream << ": " << what ();
117 
118  return (sstream.str ());
119  }
120 
121  char const*
122  what () const throw ()
123  {
124  return (message_.c_str ());
125  }
126 
127  protected:
128  std::string file_name_;
129  std::string function_name_;
130  std::string message_;
131  unsigned line_number_;
132  } ;
133 
134  /** \class InvalidConversionException
135  * \brief An exception that is thrown when a PCLPointCloud2 message cannot be converted into a PCL type
136  */
138  {
139  public:
140 
141  InvalidConversionException (const std::string& error_description,
142  const std::string& file_name = "",
143  const std::string& function_name = "" ,
144  unsigned line_number = 0) throw ()
145  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
146  } ;
147 
148  /** \class IsNotDenseException
149  * \brief An exception that is thrown when a PointCloud is not dense but is attemped to be used as dense
150  */
152  {
153  public:
154 
155  IsNotDenseException (const std::string& error_description,
156  const std::string& file_name = "",
157  const std::string& function_name = "" ,
158  unsigned line_number = 0) throw ()
159  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
160  } ;
161 
162  /** \class InvalidSACModelTypeException
163  * \brief An exception that is thrown when a sample consensus model doesn't
164  * have the correct number of samples defined in model_types.h
165  */
167  {
168  public:
169 
170  InvalidSACModelTypeException (const std::string& error_description,
171  const std::string& file_name = "",
172  const std::string& function_name = "" ,
173  unsigned line_number = 0) throw ()
174  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
175  } ;
176 
177  /** \class IOException
178  * \brief An exception that is thrown during an IO error (typical read/write errors)
179  */
180  class IOException : public PCLException
181  {
182  public:
183 
184  IOException (const std::string& error_description,
185  const std::string& file_name = "",
186  const std::string& function_name = "" ,
187  unsigned line_number = 0) throw ()
188  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
189  } ;
190 
191  /** \class InitFailedException
192  * \brief An exception thrown when init can not be performed should be used in all the
193  * PCLBase class inheritants.
194  */
196  {
197  public:
198  InitFailedException (const std::string& error_description = "",
199  const std::string& file_name = "",
200  const std::string& function_name = "" ,
201  unsigned line_number = 0) throw ()
202  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
203  } ;
204 
205  /** \class UnorganizedPointCloudException
206  * \brief An exception that is thrown when an organized point cloud is needed
207  * but not provided.
208  */
210  {
211  public:
212 
213  UnorganizedPointCloudException (const std::string& error_description,
214  const std::string& file_name = "",
215  const std::string& function_name = "" ,
216  unsigned line_number = 0) throw ()
217  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
218  } ;
219 
220  /** \class KernelWidthTooSmallException
221  * \brief An exception that is thrown when the kernel size is too small
222  */
224  {
225  public:
226 
227  KernelWidthTooSmallException (const std::string& error_description,
228  const std::string& file_name = "",
229  const std::string& function_name = "" ,
230  unsigned line_number = 0) throw ()
231  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
232  } ;
233 
235  {
236  public:
237  UnhandledPointTypeException (const std::string& error_description,
238  const std::string& file_name = "",
239  const std::string& function_name = "" ,
240  unsigned line_number = 0) throw ()
241  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
242  };
243 
245  {
246  public:
247  ComputeFailedException (const std::string& error_description,
248  const std::string& file_name = "",
249  const std::string& function_name = "" ,
250  unsigned line_number = 0) throw ()
251  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
252  };
253 
254 }
255 
256 
257 
258 #endif