Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exception handling #1119

Merged
merged 1 commit into from
Mar 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 63 additions & 64 deletions common/include/pcl/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
{ \
std::ostringstream s; \
s << message; \
s.flush (); \
throw ExceptionName(s.str(), __FILE__, BOOST_CURRENT_FUNCTION, __LINE__); \
}

Expand All @@ -67,28 +66,25 @@ namespace pcl
public:

PCLException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
: std::runtime_error (error_description)
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: std::runtime_error (createDetailedMessage (error_description,
file_name,
function_name,
line_number))
, file_name_ (file_name)
, function_name_ (function_name)
, message_ (error_description)
, line_number_ (line_number)
{
message_ = detailedMessage ();
}

virtual ~PCLException () throw ()
, line_number_ (line_number)
{}

const std::string&
const char*
getFileName () const throw ()
{
return (file_name_);
}

const std::string&
const char*
getFunctionName () const throw ()
{
return (function_name_);
Expand All @@ -100,34 +96,37 @@ namespace pcl
return (line_number_);
}

std::string
const char*
detailedMessage () const throw ()
{
std::stringstream sstream;
if (function_name_ != "")
sstream << function_name_ << " ";
return (what ());
}


protected:
static std::string
createDetailedMessage (const std::string& error_description,
const char* file_name,
const char* function_name,
unsigned line_number)
{
std::ostringstream sstream;
if (function_name != NULL)
sstream << function_name << " ";

if (file_name_ != "")
if (file_name != NULL)
{
sstream << "in " << file_name_ << " ";
if (line_number_ != 0)
sstream << "@ " << line_number_ << " ";
sstream << "in " << file_name << " ";
if (line_number != 0)
sstream << "@ " << line_number << " ";
}
sstream << ": " << what ();
sstream << ": " << error_description;

return (sstream.str ());
}

char const*
what () const throw ()
{
return (message_.c_str ());
}

protected:
std::string file_name_;
std::string function_name_;
std::string message_;

const char* file_name_;
const char* function_name_;
unsigned line_number_;
} ;

Expand All @@ -139,9 +138,9 @@ namespace pcl
public:

InvalidConversionException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

Expand All @@ -153,9 +152,9 @@ namespace pcl
public:

IsNotDenseException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

Expand All @@ -168,9 +167,9 @@ namespace pcl
public:

InvalidSACModelTypeException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

Expand All @@ -182,9 +181,9 @@ namespace pcl
public:

IOException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

Expand All @@ -196,9 +195,9 @@ namespace pcl
{
public:
InitFailedException (const std::string& error_description = "",
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

Expand All @@ -211,9 +210,9 @@ namespace pcl
public:

UnorganizedPointCloudException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

Expand All @@ -225,29 +224,29 @@ namespace pcl
public:

KernelWidthTooSmallException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

class UnhandledPointTypeException : public PCLException
{
public:
UnhandledPointTypeException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
};

class ComputeFailedException : public PCLException
{
public:
ComputeFailedException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
};

Expand All @@ -258,9 +257,9 @@ namespace pcl
{
public:
BadArgumentException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
};
}
Expand Down
8 changes: 4 additions & 4 deletions doc/advanced/content/exceptions_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Any new exception should inherit from the :pcl:`PCLException <pcl::PCLException>
{
public:
MyException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "",
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
};

Expand All @@ -40,7 +40,7 @@ For ease of use we provide this macro
{
std::ostringstream s;
s << message;
throw ExceptionName (s.str (), __FILE__, "", __LINE__);
throw ExceptionName (s.str (), __FILE__, BOOST_CURRENT_FUNCTION, __LINE__);
}

Then in your code, add:
Expand Down
12 changes: 6 additions & 6 deletions registration/include/pcl/registration/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ namespace pcl
public:

SolverDidntConvergeException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;

Expand All @@ -67,9 +67,9 @@ namespace pcl
public:

NotEnoughPointsException (const std::string& error_description,
const std::string& file_name = "",
const std::string& function_name = "" ,
unsigned line_number = 0) throw ()
const char* file_name = NULL,
const char* function_name = NULL,
unsigned line_number = 0)
: pcl::PCLException (error_description, file_name, function_name, line_number) { }
} ;
}
Expand Down