7

I'm using boost test framework 1.47 and I'm having difficulties testing 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'm trying to test code that simply throws this exception

BOOST_CHECK_THROW( source.StopCapture(), VideoCaptureException ) 

For some reason it doesn't work.

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

What is it that I'm doing wrong?

5
  • 2
    Missing semicolon after the class definition? :) Commented Nov 6, 2011 at 23:04
  • Does adding additional parenthesis around source.StopCapture() help? Commented Nov 6, 2011 at 23:06
  • @FredOverflow : The original version has semicolons =). The code compile and runs "correctly". I'm now trying to test it. Also, the parenthesis don't help Commented Nov 7, 2011 at 0:21
  • Is this a single EXE or is some of the code in DLLs? (Assuming Windows is your platform.) Commented Nov 7, 2011 at 12:59
  • I'm on Linux, I build everything in one shot so no linking other than boost Commented Nov 8, 2011 at 0:24

1 Answer 1

11

After encountering this error myself, I have tracked it down to a silly, but easy-to-make mistake:

throw new VideoCaptureException( "uh-oh" ); 

will fail with that error message, while:

throw VideoCaptureException( "uh-oh" ); 

will succeed.


The new variant causes a pointer to an exception to be caught, rather than the exception itself. The boost library doesn't know what to do with this, so it just says "unknown type".

It would be nice if the library explained the situation properly, but hopefully anybody else hitting "fatal error: unknown type" will find this page and see how to fix it!

Sign up to request clarification or add additional context in comments.

3 Comments

I would like to report that I have found this page and was able to fix it:) Taken at least an hour of my life...
Just noticed this answer. Maybe you'll reply to this comment in 4 years
It may or may not be worth noting that in versions of Boost that still have this bug, BOOST_CHECK_EXCEPTION works fine even with pointers as exceptions. Of course, to fix this, 1. update Boost, 2. don't throw exceptions as pointers (but either option may be unavailable in some awkward cases...)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.