Search code examples
c++macosshared-librariescustom-exceptions

Uncatchable custom exception C++


This has been driving me crazy all night.

class ExceptionImpl;

/**
* Custom Exception.
*/
class Exception : public virtual std::exception
{
  public:
    Exception( const Exception& original );

    Exception( const std::string& message );

    virtual ~Exception( void ) throw( );

    virtual const char* what( void ) const throw( );

  private:
    const std::unique_ptr< ExceptionImpl > m_pimpl;
};

I throw this custom exception from a library as follows

throw Exception( "Error message" );

and catch it in main via

try
{
   regex pattern(R"(a*)");

   Id::set_pattern_validator(pattern);

   assert(false);
}
catch( Exception const& exception )
{
   assert(true);
}

Id::set_pattern_validator is a static method within the Id class of the library and the source of the exception. I've tried everything i can to catch the exception but it fails to be caught.

catch( Exception )

catch( std::exception )

catch( ... )

Nada!

Terminal output is as follows.

"Terminate called after throwing an instance of 'Exception' what(): The pattern validator cannot be altered once set. Abort trap."

Now short of sacrificing a goat I'm at a loss on what to try next... any hints/tips???

Note: If i throw the custom exception within main i can catch it no problem.

Mac OS X environment using GCC with the C++0x support.

EDIT: A solution for now is to continue development on a linux based system (Fedora). I will not be accepting an answer as of yet. Thanks for everyones help.


Solution

  • If catch (...) (in an enclosing function in the same thread) doesn't handle it, your crash is not caused by a thrown an uncaught exception after all.

    (Note: It's possible for an exception to be thrown and terminate the program even though it would have been caught. Throwing from a destructor or violating a throws clause are two ways for this to happen.)