Exception checking with BOOST

I am using boost test framework 1.47 and I am having difficulty checking my exceptions.

Here is my exception class

class VideoCaptureException : public std::exception { std::string m_Description; public: VideoCaptureException(const char* description) { m_Description = std::string(description); } VideoCaptureException(const std::string& description) { m_Description = description; } virtual ~VideoCaptureException() throw() {} virtual const char* what() const throw() { return m_Description.c_str(); } } 

I am trying to check the code that just throws this exception

 BOOST_CHECK_THROW( source.StopCapture(), VideoCaptureException ) 

For some reason this does not work.

 unknown location(0): fatal error in "testVideoCaptureSource": unknown type testVideoCaptureSource.cpp(28): last checkpoint 

What am I doing wrong?

+4
source share
1 answer

After this mistake itself, I tracked it to a stupid but easy mistake:

 throw new VideoCaptureException( "uh-oh" ); 

will end with an error message, but:

 throw VideoCaptureException( "uh-oh" ); 

will be successful.


The new option calls a pointer to the exception, not the exception itself. The boost library does not know what to do with this, so it just says "unknown type".

It would be nice if the library explained the situation correctly, but I hope someone else who finds themselves in a "fatal error: unknown type" finds this page and sees how to fix it!

+3
source

Source: https://habr.com/ru/post/1379916/


All Articles