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