Point Cloud Library (PCL)  1.14.0-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 
44 /** PCL_THROW_EXCEPTION a helper macro to be used for throwing exceptions.
45  * This is an example on how to use:
46  * PCL_THROW_EXCEPTION(IOException,
47  * "encountered an error while opening " << filename << " PCD file");
48  */
49 // NOLINTBEGIN(bugprone-macro-parentheses)
50 #define PCL_THROW_EXCEPTION(ExceptionName, message) \
51 { \
52  std::ostringstream s; \
53  s << message; \
54  throw ExceptionName(s.str(), __FILE__, BOOST_CURRENT_FUNCTION, __LINE__); \
55 }
56 // NOLINTEND(bugprone-macro-parentheses)
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 char* file_name = nullptr,
71  const char* function_name = nullptr,
72  unsigned line_number = 0)
73  : std::runtime_error (createDetailedMessage (error_description,
74  file_name,
75  function_name,
76  line_number))
77  , file_name_ (file_name)
78  , function_name_ (function_name)
79  , line_number_ (line_number)
80  {}
81 
82  const char*
83  getFileName () const noexcept
84  {
85  return (file_name_);
86  }
87 
88  const char*
89  getFunctionName () const noexcept
90  {
91  return (function_name_);
92  }
93 
94  unsigned
95  getLineNumber () const noexcept
96  {
97  return (line_number_);
98  }
99 
100  const char*
101  detailedMessage () const noexcept
102  {
103  return (what ());
104  }
105 
106 
107  protected:
108  static std::string
109  createDetailedMessage (const std::string& error_description,
110  const char* file_name,
111  const char* function_name,
112  unsigned line_number)
113  {
114  std::ostringstream sstream;
115  if (function_name != nullptr)
116  sstream << function_name << " ";
117 
118  if (file_name != nullptr)
119  {
120  sstream << "in " << file_name << " ";
121  if (line_number != 0)
122  sstream << "@ " << line_number << " ";
123  }
124  sstream << ": " << error_description;
125 
126  return (sstream.str ());
127  }
128 
129  const char* file_name_;
130  const char* function_name_;
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 char* file_name = nullptr,
143  const char* function_name = nullptr,
144  unsigned line_number = 0)
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 attempted to be used as dense
150  */
152  {
153  public:
154 
155  IsNotDenseException (const std::string& error_description,
156  const char* file_name = nullptr,
157  const char* function_name = nullptr,
158  unsigned line_number = 0)
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 char* file_name = nullptr,
172  const char* function_name = nullptr,
173  unsigned line_number = 0)
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 char* file_name = nullptr,
186  const char* function_name = nullptr,
187  unsigned line_number = 0)
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 char* file_name = nullptr,
200  const char* function_name = nullptr,
201  unsigned line_number = 0)
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 char* file_name = nullptr,
215  const char* function_name = nullptr,
216  unsigned line_number = 0)
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 char* file_name = nullptr,
229  const char* function_name = nullptr,
230  unsigned line_number = 0)
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 char* file_name = nullptr,
239  const char* function_name = nullptr,
240  unsigned line_number = 0)
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 char* file_name = nullptr,
249  const char* function_name = nullptr,
250  unsigned line_number = 0)
251  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
252  };
253 
254  /** \class BadArgumentException
255  * \brief An exception that is thrown when the arguments number or type is wrong/unhandled.
256  */
258  {
259  public:
260  BadArgumentException (const std::string& error_description,
261  const char* file_name = nullptr,
262  const char* function_name = nullptr,
263  unsigned line_number = 0)
264  : pcl::PCLException (error_description, file_name, function_name, line_number) { }
265  };
266 }
An exception that is thrown when the arguments number or type is wrong/unhandled.
Definition: exceptions.h:258
BadArgumentException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:260
ComputeFailedException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:247
An exception that is thrown during an IO error (typical read/write errors)
Definition: exceptions.h:181
IOException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:184
An exception thrown when init can not be performed should be used in all the PCLBase class inheritant...
Definition: exceptions.h:196
InitFailedException(const std::string &error_description="", const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:198
An exception that is thrown when a PCLPointCloud2 message cannot be converted into a PCL type.
Definition: exceptions.h:138
InvalidConversionException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:141
An exception that is thrown when a sample consensus model doesn't have the correct number of samples ...
Definition: exceptions.h:167
InvalidSACModelTypeException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:170
An exception that is thrown when a PointCloud is not dense but is attempted to be used as dense.
Definition: exceptions.h:152
IsNotDenseException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:155
An exception that is thrown when the kernel size is too small.
Definition: exceptions.h:224
KernelWidthTooSmallException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:227
A base class for all pcl exceptions which inherits from std::runtime_error.
Definition: exceptions.h:66
const char * detailedMessage() const noexcept
Definition: exceptions.h:101
const char * file_name_
Definition: exceptions.h:129
const char * getFileName() const noexcept
Definition: exceptions.h:83
PCLException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:69
unsigned line_number_
Definition: exceptions.h:131
static std::string createDetailedMessage(const std::string &error_description, const char *file_name, const char *function_name, unsigned line_number)
Definition: exceptions.h:109
const char * getFunctionName() const noexcept
Definition: exceptions.h:89
const char * function_name_
Definition: exceptions.h:130
unsigned getLineNumber() const noexcept
Definition: exceptions.h:95
UnhandledPointTypeException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:237
An exception that is thrown when an organized point cloud is needed but not provided.
Definition: exceptions.h:210
UnorganizedPointCloudException(const std::string &error_description, const char *file_name=nullptr, const char *function_name=nullptr, unsigned line_number=0)
Definition: exceptions.h:213