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