Point Cloud Library (PCL)  1.14.1-dev
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 
38 #pragma once
39 
40 #include <stdexcept>
41 #include <sstream>
42 #include <boost/current_function.hpp>
43 #include <pcl/pcl_exports.h> // for PCL_EXPORTS
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 // NOLINTBEGIN(bugprone-macro-parentheses)
51 #define PCL_THROW_EXCEPTION(ExceptionName, message) \
52 { \
53  std::ostringstream s; \
54  s << message; \
55  throw ExceptionName(s.str(), __FILE__, BOOST_CURRENT_FUNCTION, __LINE__); \
56 }
57 // NOLINTEND(bugprone-macro-parentheses)
58 
59 namespace pcl
60 {
61 
62  /** \class PCLException
63  * \brief A base class for all pcl exceptions which inherits from std::runtime_error
64  * \author Eitan Marder-Eppstein, Suat Gedikli, Nizar Sallem
65  */
66  class PCL_EXPORTS PCLException : public std::runtime_error
67  {
68  public:
69 
70  PCLException (const std::string& error_description,
71  const char* file_name = nullptr,
72  const char* function_name = nullptr,
73  unsigned line_number = 0)
74  : std::runtime_error (createDetailedMessage (error_description,
75  file_name,
76  function_name,
77  line_number))
78  , file_name_ (file_name)
79  , function_name_ (function_name)
80  , line_number_ (line_number)
81  {}
82 
83  const char*
84  getFileName () const noexcept
85  {
86  return (file_name_);
87  }
88 
89  const char*
90  getFunctionName () const noexcept
91  {
92  return (function_name_);
93  }
94 
95  unsigned
96  getLineNumber () const noexcept
97  {
98  return (line_number_);
99  }
100 
101  const char*
102  detailedMessage () const noexcept
103  {
104  return (what ());
105  }
106 
107 
108  protected:
109  static std::string
110  createDetailedMessage (const std::string& error_description,
111  const char* file_name,
112  const char* function_name,
113  unsigned line_number)
114  {
115  std::ostringstream sstream;
116  if (function_name != nullptr)
117  sstream << function_name << " ";
118 
119  if (file_name != nullptr)
120  {
121  sstream << "in " << file_name << " ";
122  if (line_number != 0)
123  sstream << "@ " << line_number << " ";
124  }
125  sstream << ": " << error_description;
126 
127  return (sstream.str ());
128  }
129 
130  const char* file_name_;
131  const char* function_name_;
132  unsigned line_number_;
133  } ;
134 
135  /** \class InvalidConversionException
136  * \brief An exception that is thrown when a PCLPointCloud2 message cannot be converted into a PCL type
137  */
139  {
140  public:
141 
142  InvalidConversionException (const std::string& error_description,
143  const char* file_name = nullptr,
144  const char* function_name = nullptr,
145  unsigned line_number = 0)
146  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
147  } ;
148 
149  /** \class IsNotDenseException
150  * \brief An exception that is thrown when a PointCloud is not dense but is attempted to be used as dense
151  */
153  {
154  public:
155 
156  IsNotDenseException (const std::string& error_description,
157  const char* file_name = nullptr,
158  const char* function_name = nullptr,
159  unsigned line_number = 0)
160  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
161  } ;
162 
163  /** \class InvalidSACModelTypeException
164  * \brief An exception that is thrown when a sample consensus model doesn't
165  * have the correct number of samples defined in model_types.h
166  */
168  {
169  public:
170 
171  InvalidSACModelTypeException (const std::string& error_description,
172  const char* file_name = nullptr,
173  const char* function_name = nullptr,
174  unsigned line_number = 0)
175  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
176  } ;
177 
178  /** \class IOException
179  * \brief An exception that is thrown during an IO error (typical read/write errors)
180  */
182  {
183  public:
184 
185  IOException (const std::string& error_description,
186  const char* file_name = nullptr,
187  const char* function_name = nullptr,
188  unsigned line_number = 0)
189  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
190  } ;
191 
192  /** \class InitFailedException
193  * \brief An exception thrown when init can not be performed should be used in all the
194  * PCLBase class inheritants.
195  */
197  {
198  public:
199  InitFailedException (const std::string& error_description = "",
200  const char* file_name = nullptr,
201  const char* function_name = nullptr,
202  unsigned line_number = 0)
203  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
204  } ;
205 
206  /** \class UnorganizedPointCloudException
207  * \brief An exception that is thrown when an organized point cloud is needed
208  * but not provided.
209  */
211  {
212  public:
213 
214  UnorganizedPointCloudException (const std::string& error_description,
215  const char* file_name = nullptr,
216  const char* function_name = nullptr,
217  unsigned line_number = 0)
218  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
219  } ;
220 
221  /** \class KernelWidthTooSmallException
222  * \brief An exception that is thrown when the kernel size is too small
223  */
225  {
226  public:
227 
228  KernelWidthTooSmallException (const std::string& error_description,
229  const char* file_name = nullptr,
230  const char* function_name = nullptr,
231  unsigned line_number = 0)
232  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
233  } ;
234 
236  {
237  public:
238  UnhandledPointTypeException (const std::string& error_description,
239  const char* file_name = nullptr,
240  const char* function_name = nullptr,
241  unsigned line_number = 0)
242  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
243  };
244 
246  {
247  public:
248  ComputeFailedException (const std::string& error_description,
249  const char* file_name = nullptr,
250  const char* function_name = nullptr,
251  unsigned line_number = 0)
252  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
253  };
254 
255  /** \class BadArgumentException
256  * \brief An exception that is thrown when the arguments number or type is wrong/unhandled.
257  */
259  {
260  public:
261  BadArgumentException (const std::string& error_description,
262  const char* file_name = nullptr,
263  const char* function_name = nullptr,
264  unsigned line_number = 0)
265  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
266  };
267 }
An exception that is thrown when the arguments number or type is wrong/unhandled.
Definition: exceptions.h:259
BadArgumentException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:261
ComputeFailedException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:248
An exception that is thrown during an IO error (typical read/write errors)
Definition: exceptions.h:182
IOException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:185
An exception thrown when init can not be performed should be used in all the PCLBase class inheritant...
Definition: exceptions.h:197
InitFailedException(const std::string &error_description="", const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:199
An exception that is thrown when a PCLPointCloud2 message cannot be converted into a PCL type.
Definition: exceptions.h:139
InvalidConversionException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:142
An exception that is thrown when a sample consensus model doesn't have the correct number of samples ...
Definition: exceptions.h:168
InvalidSACModelTypeException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:171
An exception that is thrown when a PointCloud is not dense but is attempted to be used as dense.
Definition: exceptions.h:153
IsNotDenseException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:156
An exception that is thrown when the kernel size is too small.
Definition: exceptions.h:225
KernelWidthTooSmallException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:228
A base class for all pcl exceptions which inherits from std::runtime_error.
Definition: exceptions.h:67
const char * detailedMessage() const noexcept
Definition: exceptions.h:102
const char * file_name_
Definition: exceptions.h:130
const char * getFileName() const noexcept
Definition: exceptions.h:84
PCLException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:70
unsigned line_number_
Definition: exceptions.h:132
static std::string createDetailedMessage(const std::string &error_description, const char *file_name, const char *function_name, unsigned line_number)
Definition: exceptions.h:110
const char * getFunctionName() const noexcept
Definition: exceptions.h:90
const char * function_name_
Definition: exceptions.h:131
unsigned getLineNumber() const noexcept
Definition: exceptions.h:96
UnhandledPointTypeException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:238
An exception that is thrown when an organized point cloud is needed but not provided.
Definition: exceptions.h:211
UnorganizedPointCloudException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:214
#define PCL_EXPORTS
Definition: pcl_macros.h:323