I’m working on an issue whereby a corrupt input file is causing an exception to be thrown. The exception class is defined in an implementation file and thus not visible to me. It does inherit from std::exception
.
I tried simply forward declaring the exception class, since I’m just catching it by reference. However, this gave me a error: invalid use of incomplete type
compiler error (using GCC 6.2 on Linux). I suppose that the compiler needs the full exception object type so that it can rethrow the exception if needed.
So this is what I’d like to do:
// library.cpp
namespace FOO {
struct SomeException : public std::exception
{
// string member, virtual dtor, ctor taking one arg and virtual what()
};
void doStuff() {
}
}
// my main.cpp
namespace FOO
{
struct SomeException;
}
int main()
{
try
{
FOO::doStuff();
}
catch (FOO::SomeException& e)
{
// ignore e, but I know where it came from so log
// an appropriate message
}
catch (std::exception& e)
{
// most other exceptions, log `what()` message
}
catch(...)
{
// tell user to contact customer support
}
}
Just printing the what()
message isn’t appropriate to my context.
I could ask the other team to move their exception class definition into a header. That will probably be a slow process. I suppose I could also do a string comparison on the what()
message, but that seems ugly.
Are there any other options?
(BTW I can’t see any mention of this via Google, but this does seem to be a sort of anti-pattern, the “throw-only exception”).